BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SK_11
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20

> The solution should work for any number of columns and rows.

What defines the dimensions of the array?

Tom
Super User Tom
Super User

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).

japelin
Rhodochrosite | Level 12

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;

 

Tom
Super User Tom
Super User

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

PGStats
Opal | Level 21
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;

PGStats_0-1630551891550.png

 

PG
SK_11
Obsidian | Level 7

Thank you all.

Ksharp
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2331 views
  • 0 likes
  • 6 in conversation