data long_data;
input team $ variable $ value;
datalines;
A Points 88
A Assists 12
A Rebounds 22
B Points 91
B Assists 17
B Rebounds 28
C Points 99
C Assists 24
C Rebounds 30
D Points 94
D Assists 28
D Rebounds 31
;
run;
proc transpose data=long_data out=wide_data name=variables;
by team;
id variable;
var value;
run;
One way is to use the data step.
proc sort data=long_data out=sorted;
by team;
run;
data wide_data2;
set sorted;
by team;
retain points assists rebounds;
if first.team then do;
Points=.;
Assists=.;
Rebounds=.;
end;
if variable='Points' then Points=value;
if variable='Assists' then Assists=value;
if variable='Rebounds' then Rebounds=value;
if last.team;
keep team points assists rebounds;
run;
@BrahmanandaRao wrote:
Using array method is it possible
What variable would you use as the index into the array?
data long_data;
input team $ variable $ value;
datalines;
A Points 88
A Assists 12
A Rebounds 22
B Points 91
B Assists 17
B Rebounds 28
C Points 99
C Assists 24
C Rebounds 30
D Points 94
D Assists 28
D Rebounds 31
;
run;
proc sql noprint;
select distinct catt('long_data(where=(variable="',variable,'") rename=(value=',variable,'))')
into :merge separated by ' '
from long_data;
quit;
data want;
merge &merge.;
by team;
drop variable;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.