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

I want to make my data wide format while keeping the count (CNT) and percent (PCT) as columns.

Data Have >> Want

mariko5797_0-1629304529430.png

 

Code Attempted

 

/* attempt 1*/
proc transpose data = CohortA out = CohortA_wide prefix = A_;
 var CNT PCT;
 by VAR;
 id BMI;
run;

/* attempt 2*/
proc transpose data = CohortA out = CohortA_wide prefix = A_;
 by VAR;
 id BMI;
run;

The issue with attempt 1 is that CNT and PCT become categories within the same column, which is not what I want.

The issue with attempt 2 is that only CNT is shown. 

 

Is the only way to PROC TRANSPOSE twice and then merge? Or is there a way with a single PROC TRANSPOSE?

 

Thank you in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Within PROC TRANSPOSE the only solution is to transpose twice and then merge, or transpose twice, one to a super long format then to a wide format.

You can do it in a single data step though. 

 

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

 


@mariko5797 wrote:

I want to make my data wide format while keeping the count (CNT) and percent (PCT) as columns.

Data Have >> Want

mariko5797_0-1629304529430.png

 

Code Attempted

 

/* attempt 1*/
proc transpose data = CohortA out = CohortA_wide prefix = A_;
 var CNT PCT;
 by VAR;
 id BMI;
run;

/* attempt 2*/
proc transpose data = CohortA out = CohortA_wide prefix = A_;
 by VAR;
 id BMI;
run;

The issue with attempt 1 is that CNT and PCT become categories within the same column, which is not what I want.

The issue with attempt 2 is that only CNT is shown. 

 

Is the only way to PROC TRANSPOSE twice and then merge? Or is there a way with a single PROC TRANSPOSE?

 

Thank you in advance.

 


 

View solution in original post

1 REPLY 1
Reeza
Super User

Within PROC TRANSPOSE the only solution is to transpose twice and then merge, or transpose twice, one to a super long format then to a wide format.

You can do it in a single data step though. 

 

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

 


@mariko5797 wrote:

I want to make my data wide format while keeping the count (CNT) and percent (PCT) as columns.

Data Have >> Want

mariko5797_0-1629304529430.png

 

Code Attempted

 

/* attempt 1*/
proc transpose data = CohortA out = CohortA_wide prefix = A_;
 var CNT PCT;
 by VAR;
 id BMI;
run;

/* attempt 2*/
proc transpose data = CohortA out = CohortA_wide prefix = A_;
 by VAR;
 id BMI;
run;

The issue with attempt 1 is that CNT and PCT become categories within the same column, which is not what I want.

The issue with attempt 2 is that only CNT is shown. 

 

Is the only way to PROC TRANSPOSE twice and then merge? Or is there a way with a single PROC TRANSPOSE?

 

Thank you in advance.

 


 

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
  • 1 reply
  • 531 views
  • 0 likes
  • 2 in conversation