BookmarkSubscribeRSS Feed
hellohere
Pyrite | Level 9

Get data structure below. How to transpose once for all variables?! 

proc transpose data=have out=need;

by seq_id;

id time;

var var1 var2 var3;

run;quit;

 

the outcome is stacked for all variables. 

OR anyway to do this by SQL or data step?

 

time	seq_id	var1	var2	var3 	…
1120	1	…			
1120	2	…			
1120	3	…			
1120	4	…			
1125	1	…			
1125	2	…			
1125	3	..			
1125	4	…			
5 REPLIES 5
Patrick
Opal | Level 21

Below article explains and provides macros for transposing that exceed what a single Proc Transpose can do.
Transpose your analysis data with the %MAKELONG and %MAKEWIDE macro

 

The macros are available from within the article.

Patrick_0-1702088950440.png

 

Tom
Super User Tom
Super User

I don't understand.  Why would you create a variable using TIME as the basis for the name of the variable?
What use is that going to be to you?  How will you be able to calculate time differences if you have stored the time value into the NAME of a variable instead of leaving it in a the VALUE of variable where it can be referenced?

Kurt_Bremser
Super User

Since the result would be unusable for further analysis, I take it this is for reporting purposes.

proc report data=have;
column seq_id (var1 var2 var3),time;
define seq_id / group;
define var1 / analysis;
define var2 / analysis;
define var3 / analysis;
define time / "" across;
run;
Ksharp
Super User

Check my paper - MERGE Skill to transpose data.

https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf

 

/*
Try the MERGE skill proposed by me.
And that would be better if you post the result you are looking for.
*/

data have;
infile cards truncover expandtabs;
input time	seq_id	var1	var2	var3;
cards;
1120	1	1  2  3
1120	2	1  2  3
1120	3	1  2  3
1120	4	1  2  3
1125	1	1  2  3
1125	2	1  2  3
1125	3	1  2  3
1125	4	1  2  3
;

proc sql noprint;
select distinct catt('have(where=(time=',time,') rename=(var1=var1_',time,' var2=var2_',time,' var3=var3_',time,'))')
       into :merge separated by ' '
 from have;
quit;
data want;
 merge &merge.;
 by seq_id;
 drop time;
run;

Ksharp_0-1702117611036.png

 

kimmorgan
Fluorite | Level 6

Thanks for the information!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 561 views
  • 0 likes
  • 6 in conversation