I would like to convert the following list into 2D Array.
a |
b |
c |
d |
e |
The output should look like
a | b |
c | d |
e |
or
a | b | c |
d | e |
.
The solution should work for any number of columns and rows.
Any help would be highly appreciated.
If you are talking about converting one dataset to another dataset then here is simple method, but I wouldn't call either dataset an array.
Just specify the number of new variables (columns) you want to create. The data step will generate the appropriate number of observations (rows) based on how many observations are in the input dataset.
data have;
input var $;
cards;
a
b
c
d
e
;
%let cols=3;
data want;
do col=1 to &cols until(eof);
set have end=eof;
array cols $8 col1-col&cols;
cols[col]=var;
end;
drop col var;
run;
proc print;
run;
Obs col1 col2 col3 1 a b c 2 d e
> The solution should work for any number of columns and rows.
What defines the dimensions of the array?
What do you mean by "array"?
Are you talking about and ARRAY in a SAS data step? What variables to you want to use the ARRAY statement to reference? Once you have the list of variables it does not matter what set of dimensions you use.
Or are you using PROC IML? Sounds like you just want to reshape an single column array into a multi-column array.
Or are you talking about create a SAS dataset (which NOT an array).
try this code.
data have;
length a $1;
input a $;
cards;
a
b
c
d
e
;
run;
data want;
set have end=eof;
/* specify number of elements */
array x{3} $1;
retain x:;
/* initialization */
if mod(_n_,dim(x))=1 then do;
do _i=1 to dim(x);
x{_i}='';
end;
end;
/* value to array */
_ord=mod(_n_,dim(x));
x{ifn(_ord=0,dim(x),_ord)}=a;
/* output obs */
if eof then do;
output;
end; else
if mod(_n_,dim(x))=0 then do;
output;
end;
drop a _:;
run;
If you are talking about converting one dataset to another dataset then here is simple method, but I wouldn't call either dataset an array.
Just specify the number of new variables (columns) you want to create. The data step will generate the appropriate number of observations (rows) based on how many observations are in the input dataset.
data have;
input var $;
cards;
a
b
c
d
e
;
%let cols=3;
data want;
do col=1 to &cols until(eof);
set have end=eof;
array cols $8 col1-col&cols;
cols[col]=var;
end;
drop col var;
run;
proc print;
run;
Obs col1 col2 col3 1 a b c 2 d e
data have;
do t = "a", "b", "c", "d", "e";
output;
end;
run;
data _null_;
if 0 then set have nobs=nobs;
call symputx("n", floor(sqrt(nobs))); /* replace floor by ceil for more columns */
stop;
run;
data want;
array tt{&n.} $;
do i = 1 to &n. until(done);
set have end=done;
tt{i} = t;
end;
keep tt:;
run;
proc print data=want; run;
Thank you all.
It is IML 's thing .
data have;
length a $1;
input a $;
cards;
a
b
c
d
e
;
run;
proc iml;
use have;
read all var _all_ into x;
close;
want=shape(x,3,2,' ');
print want;
want=shape(x,2,3,' ');
print want;
quit;
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.