SAS count of variables - count

%let var= A B C D E; when I do the do loop %do i=1 %to 5; instead of hard code 5, I want to use count(A B C D E) or something similar. Because in the future, var may change to A B C D, then 5 will become 4. Need your help. Thank you.

Use COUNTW() with a fully worked example of exactly what you're attempting in the SAS Macro Appendix:
%macro iterm(lst);
%let finish=%sysfunc(countw(&lst));
%do i = 1 %to &finish;
%put %scan(&lst,&i);
%end;
%mend iterm;
%iterm(a c e)

Related

Multiple outputs used repeatedly in for loop Julia

I am using Julia and I've designed a for loop that takes the outputs of a function in one loop and uses them as the input of that function in the next loop (and over and over). When I run this code, Julia flags an "undefined" error, however, if I run the code in debug mode, it executes perfectly. For example, the code looks like this:
function do_command(a,b,c,d)
a = a + 1
b = split(b, keepempty=false)[1]
c = split(b, keepempty=false)[1]
if a == 1000
d = true
else
d = false
end
return a, b, c, d
end
for ii in 1:length(x)
if ii == 1
a = 0
b = "string something"
c = ""
d = false
end
a,b,c,d = do_command(a,b,c,d)
if d == true
print(string(b))
break
end
end
What am I doing wrong here?
An issue with your code is that for introduces a new scope for each iteration of the loop. That is to say: variable a created within the loop body at iteration 1 is not the same as variable a created within the loop body at iteration 2.
In order to fix your problem, you should declare variables outside the loop, so that at each iteration, references to them from within the loop body would actually refer to the same variables from the enclosing scope.
I'd go with something like this:
function do_command(a,b,c,d)
a = a + 1
b = split(b, keepempty=false)[1]
c = split(b, keepempty=false)[1]
if a == 1000
d = true
else
d = false
end
return a, b, c, d
end
# Let's create a local scope: it's good practice to avoid global variables
let
# All these variables are declared in the scope introduced by `let`
a = 0
b = "string something"
c = ""
d = false
for ii in 1:10 #length(x)
# now these names refer to the variables declared in the enclosing scope
a,b,c,d = do_command(a,b,c,d)
if d == true
print(string(b))
break
end
end
end

R: For loop only works when manually executed

so I have a dataframe with four number colums A,B,C and D
I now wrote a for-loop to change values row by row when certain conditions are met.
but nothing happens! If I manually set k=1, k=2, k=3 and run the two if functions in the loop manually it works! This seems like a bug to me?
for (k in nrow(list)) {
if(list$A[k]>list$C[k]) {list$C[k]=list$A[k]}
if(list$B[k]<list$D[k]) {list$D[k]=list$B[k]}
}
list
Example:
A B C D
195679832 197768053 195000001 197500000
195679832 197768053 197500001 200000000
227573015 228592110 227500001 230000000
64199445 65230121 65000001 67500000
should become
A B C D
195679832 197768053 195679832 197500000
195679832 197768053 197500001 197768053
227573015 228592110 227573015 228592110
64199445 65230121 64199445 65000000
64199445 65230121 65000001 65230121
again: when I manually increase k and run the content of the loop one by one it works just fine, but if i run the loop as a whole it wont execute the if clauses anymore. It gives no error message aswell.
You forgott the 1: before nrow(list). k was only receiving 4, not 1:4.
for (k in 1:nrow(list)) {
if(list$A[k]>list$C[k]) {list$C[k]=list$A[k]}
if(list$B[k]<list$D[k]) {list$D[k]=list$B[k]}
}
list

Printing variable subscripts in Julia

Hey so I know that due to the Unicode support in Julia one, may write for instance the letter a with the subscript 1 by typing a\_1<TAB>. Now, what if I wanted to do something like the following:
for i in [1 2 3]
println("a\_i")
end
and have the output be written as
a₁
a₂
a₃
How would I go about this without writing out all the possible subscripts myself?
You could do this (at least in version 0.6):
ltx = Base.REPLCompletions.latex_symbols
for i in 1:3
println("a$(ltx["\\_$i"])")
end
Bogumił Kamiński's answer seems the neatest, but I needed to reverse the order to get the correct string for two-digit numbers:
subscript(i::Integer) = i<0 ? error("$i is negative") : join('₀'+d for d in reverse(digits(i)))
for i=7:13 println("a"*subscript(i)) end
Building off the other answers here, I wrote a set of functions to allow for negative numbers and to work with the more complex superscript case.
function subscriptnumber(i::Int)
if i < 0
c = [Char(0x208B)]
else
c = []
end
for d in reverse(digits(abs(i)))
push!(c, Char(0x2080+d))
end
return join(c)
end
function superscriptnumber(i::Int)
if i < 0
c = [Char(0x207B)]
else
c = []
end
for d in reverse(digits(abs(i)))
if d == 0 push!(c, Char(0x2070)) end
if d == 1 push!(c, Char(0x00B9)) end
if d == 2 push!(c, Char(0x00B2)) end
if d == 3 push!(c, Char(0x00B3)) end
if d > 3 push!(c, Char(0x2070+d)) end
end
return join(c)
end
julia> for i in [1 -2 39]
println("a"*superscriptnumber(i))
println("a"*subscriptnumber(i))
end
a¹
a₁
a⁻²
a₋₂
a³⁹
a₃₉

How to break out of nested for loops in Julia

I have tried to break out of nested loops in a quite ineffective way:
BreakingPoint = false
a=["R1","R2","R3"]
b=["R2","R3","R4"]
for i in a
for j in b
if i == j
BreakingPoint = true
println("i = $i, j = $j.")
end
if BreakingPoint == true; break; end
end
if BreakingPoint == true; break; end
end
Is there an easier way to do that? In my actual problem, I have no idea about what are in arrays a and b, apart from they are ASCIIStrings. The array names (a and b in sample code) are also auto-generated through meta-programming methods.
You can do one of two things
have the loop statement (if thats what its called) in a multi outer loop
for i in a, j in b
if i == j
break
end
end
which is clean, but not always possible
I will be crucified for suggesting this, but you can use #goto and #label
for i in a
for j in b
if i == j
#goto escape_label
end
end
end
#label escape_label
If you go with the #goto/#label way, for the sake of the people maintaining/reviewing the code, document your use properly, as navigating code with labels is breathtakingly annoying
For the discussion on the multi-loop break, see this
Put the 2D loop into a function, and do an early return when you want to break.

Pivot datatable based on key and value in datatable

the data i am having is like, i am trying this from week am not able to find any solution.
I need to do this using either pivot using c# or using lists. Kindly provide the answers. immediate.
Key Value
A e
B f
C i
D j
A k
B l
C m
D n
i need something like this
A B C D
-------
e f i j
k l m n
where the first line is a column header.
Any better link of any stackoverflow question also appreciable.
By following code am able to get distinct columns, now problem is with assigning values in rows.
please provide any better way of doing this.
foreach (DataRow row in dtBackupData.Rows)
{
keyname = row["KeyName"].ToString();
if (!keyNameList.Contains(keyname))
{
keyNameList.Add(keyname);
dtDynamic.Columns.Add(keyname, typeof(string));
}
}

Resources