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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.