BookmarkSubscribeRSS Feed
nyy2
Fluorite | Level 6

hello--

I have a wide dataset (100 columns) that I would like to transpose by patientID to long. I have 30 columns that all have the same prefix diag_1,diag_2, etc.

 

Is there a way to write macro within a proc sql loop that would select union through each of the diag_ variables?

 

tia!

3 REPLIES 3
Reeza
Super User
Why not use PROC TRANSPOSE instead?
ballardw
Super User

What are the other 69 columns? Or do you not want them included in the long set?

Does PatientId repeat in the data, such as on different dates or different facilities? Is the date/facility/other information to be in the result?

 

Assuming that you have a date along with the patient Id this is how I would start this:

Proc sort data=have;
   by PatientId Visitdate;
run;

Proc transpose data=have out=long;
   by patientid visitdate;
   var diag_: ;
run;

You want to include enough variables in the BY statement in both Proc Sort and transpose to uniquely identify the source record. The Diag_: is a list that tells SAS to use all the variables whose names start with Diag_ . If there are some that are different than your numbered variables that you would not want then use the list Diag_1 - Diag_30 (assumes they are sequentially numbered).

Reeza
Super User

Basically there's a few ways to transpose data and SQL is the last option in the list of options by preference because you have to manually do everything. Using a data step and array, proc transpose are the best options.

 

Transposing data tutorials:
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/

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 3 replies
  • 332 views
  • 2 likes
  • 3 in conversation