BookmarkSubscribeRSS Feed
Tim_sas_
Obsidian | Level 7

Hello,

 

I have a very basic question regarding a table where I would like to change the order of rows and columns.

The problem is illustrated by the picture below. I have a SAS table in the upper format and would like to transform it to the format describes below.

 

I would be very glad if some could help me out.

 

Thanks a lot.

 

Peter

 

 

 

Pic.JPG

 

6 REPLIES 6
Reeza
Super User

This is called a transpose.

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

Untested but this should be pretty close. 

proc sort data=have;
by startYear variable;
run;

proc transpose data=have out=want;
by startYear;
ID variable;
value Estimate;
run;

@Tim_sas_ wrote:

Hello,

 

I have a very basic question regarding a table where I would like to change the order of rows and columns.

The problem is illustrated by the picture below. I have a SAS table in the upper format and would like to transform it to the format describes below.

 

I would be very glad if some could help me out.

 

Thanks a lot.

 

Peter

 

 

 

Pic.JPG

 




Tim_sas_
Obsidian | Level 7
Thank you very much, this was very helpful!
PGStats
Opal | Level 21

Try this:

 

proc sort data=myData; by startYear variable; run;

proc transpose data=myData out= want(drop=_name_);
by startYear;
var estimate;
id variable;
run;

(untested)

PG
Tim_sas_
Obsidian | Level 7
Thanks a lot, your code worked excellently. Thank you.
ballardw
Super User

If all of the values of your Variable are acceptable as SAS variable names this should do it:

Assumes the data is sorted by startyear. If it isn't sorted then sort before the proc transpose.

Proc transpose data=have 
   out=want;
   by startyear;
   id variable;
   var estimate;
run;

This will however fail of any startyear value has multiple rows with the same value of Variable.

andreas_lds
Jade | Level 19

Could be a job for proc report using across, if you don't need a dataset, but a report.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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