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

Hello. I am new to learn the array. I know array can restructure the data from horizontal to vertical, but I don't know how can I transfer my data? This is my code:

data q11;
input q1-q10;

cards;
1 2 3 6 5 3 7 4 7 9
1 2 3 9 5 3 7 3 9 8
1 2 9 6 5 2 7 3 7 8
;run;

data store;
set q11;
array store{10}q1-q10;
do i=1 to 10;
transfer=store{i};
output;
end;
drop q1-q10; 
run;
my code outputmy code outputdesird structuredesird structure

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @shawn123   


data q11;
input q1-q10;

cards;
1 2 3 6 5 3 7 4 7 9
1 2 3 9 5 3 7 3 9 8
1 2 9 6 5 2 7 3 7 8
;run;
data want;
 do _n_=1 by 1 until(z);
  set q11 end=z;
  array t(9999,9999) _temporary_;
  array j q1-q10;
  do over j;
   t(_n_,_i_)=j;
  end;
 end;
 array want(3);
 do over j;
  do _n_=1 to dim(want);
   want(_n_)=t(_n_,_i_);
  end;
  output;
 end;
 drop q:;
run;

View solution in original post

6 REPLIES 6
shawn123
Obsidian | Level 7

array rescturing question.PNGarray rescturing question1.PNG( I want this format)

novinosrin
Tourmaline | Level 20

Hi @shawn123   


data q11;
input q1-q10;

cards;
1 2 3 6 5 3 7 4 7 9
1 2 3 9 5 3 7 3 9 8
1 2 9 6 5 2 7 3 7 8
;run;
data want;
 do _n_=1 by 1 until(z);
  set q11 end=z;
  array t(9999,9999) _temporary_;
  array j q1-q10;
  do over j;
   t(_n_,_i_)=j;
  end;
 end;
 array want(3);
 do over j;
  do _n_=1 to dim(want);
   want(_n_)=t(_n_,_i_);
  end;
  output;
 end;
 drop q:;
run;
shawn123
Obsidian | Level 7
Thank you so much!
Tom
Super User Tom
Super User

PROC TRANSPOSE is designed to do that.

proc transpose data=q11 out=flop prefix=x ;
  var q1-q10;
run;

proc print data=flop;
run;
Obs    _NAME_    x1    x2    x3

  1     q1        1     1     1
  2     q2        2     2     2
  3     q3        3     3     9
  4     q4        6     9     6
  5     q5        5     5     5
  6     q6        3     3     2
  7     q7        7     7     7
  8     q8        4     3     3
  9     q9        7     9     7
 10     q10       9     8     8
shawn123
Obsidian | Level 7
That's a good idea! just want to make sure, the character variable can also do the transpose right?
Tom
Super User Tom
Super User

@shawn123 wrote:
That's a good idea! just want to make sure, the character variable can also do the transpose right?

If you want to include character variable(s) in the list of variables to transpose then you must include the VAR statement. Without it PROC TRANSPOSE will just transpose the numeric variables.  Also if you transpose a mix of numeric and character variables then the numeric values will be converted to character.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 875 views
  • 3 likes
  • 3 in conversation