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

I'm trying to write a macro that can take any data set and any key variables and convert from wide form to long form. I'm having a hard time visualizing how this is possible. 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
4 REPLIES 4
matoma
Obsidian | Level 7

I eventually want to apply it to this data set Annotation 2020-04-25 134027.png

Reeza
Super User

That would be a PROC TRANSPOSE. 

 

proc transpose data=have out=want;
by ID GENDER;
VAR age: ;
run;

I suppose you could make it a macro but I don't see a huge amount of value in that beyond using the procedure. Here are some tutorials on transposing if you want to understand the different option in SAS further. There are also many user macros already written, this is one written by some of the users here. 

https://communities.sas.com/t5/SAS-Communities-Library/A-better-way-to-FLIP-i-e-transpose-make-wide-...

 

Transposing data tutorials:
Long to Wide:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/

https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/

Wide to Long:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/

https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/

And sometimes a double transpose is needed for extra wide data sets:
https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd

Tom
Super User Tom
Super User

Before writing the macro think about what SAS code you want to run.  Then you can start thinking about how to generate that code using macro logic.

 

Are you asking for something different than PROC TRANSPOSE.

proc transpose data=have out=want ;
  by key1 key2 ;
  var _all_ ;
run;
PaigeMiller
Diamond | Level 26

Don't write your own macro. Use the %UNTRANSPOSE macro

https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2419-2018.pdf

--
Paige Miller

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
  • 4 replies
  • 686 views
  • 0 likes
  • 4 in conversation