How to efficently create arrays with runs of equal elements in SAS -
case one
sample output:
`1,1,1,1,2,2,2,2......9,9,9,9,0,0,0,0'
a more general case be:
the array starts n1
elements valued x1
, followed n2
elements valued x2
...
in sample output, n1 = n2 = n3 = .. = 4
, x1=1, x2=2 ...
but don't want create based on element's position in array using if-else
statement.
here's have done:
%let nd = 80; data _t(drop = i); array ap{&nd}; = 1 &nd; if le 4 a[i] = 1; else ....; end; 'other codes' run;
case two
what if order in array doesn't matter long contains elements need (n1 x1, n2 x2 ...) ? in scenario, easier build array?
provided know in advance upper bound how many elements want create, can in array statement - e.g.
data _null_; array t{10} (1*1 2*2 3*3 4*4); put t{*}; run;
output:
1 2 2 3 3 3 4 4 4 4
n.b. sort of assignment implicitly causes array variables retained.
you can nest brackets when creating runs of elements, e.g,
data _null_; array t{10} (2*(1 2 3 4 5)); put t{*}; run;
output:
1 2 3 4 5 1 2 3 4 5
however, *
signs need separated brackets, e.g.
data _null_; array t{10} (2*(1 2) 2*(3*3)); put t{*}; run;
output:
1 2 1 2 3 3 3 3 3 3
Comments
Post a Comment