PROC TRANSPOSE! It goes both ways 😉
Long to Wide
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/
Wide to Long
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/
@Amanda_Lemon wrote:
One more question -- is there a similarly easy way to do the reverse procedure, i.e., transform a wide data set into a tall one?
It works! Thank you!!
A follow-up question: I am not quite sure which of my variables are attributes of a person (i.e., don't vary within a person between Time 1 and Time 2) and which are attributes of a test (different values for Time 1 and Time 2 for the same person). Is there an easy way to find out which variables are which?
The best option I found so far is to split the file into two: one file with Time 1 values and another file with Time 2 values, and then use proc compare. It gives me a table with unequal values... but I thought maybe there is a way that I can get a LIST of variables with unequal values and a LIST of variables with equal values so that it's easier to copy-paste in proc transpose? (I have about 600 variables, so I am really looking to automatize this process...).
Any ideas would be highly appreciated!
Working on a very similar problem at the moment. I test it out by checking if the max=min for the variables in question across the person ID.
This is easier done in SQL than a data step because PROC SQL will allow the max/min functions to be used on character variables.
You could play with this approach
proc means data=have noprint;
by id;
var _all_;
output out=want(drop= _:) std=;
run;
proc transpose data=want(obs=1) out=w;
run;
proc sql;
select _name_ into :by_group separated by ' '
from w
where col1 = 0;
select _name_ into :tranpose_var separated by ' '
from w
where col1 ne 0;
quit;
/*by group and transpose lists*/
%put &by_group;
%put &tranpose_var;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.