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

I have this wide dataset structure. 

data have;
	input ID 7. CO2 Methane N2O HFC;
	datalines;
1004377 54 28 54 30
1000112 88 40 72 1
;

And I want this long dataset structure. 

data want;
	input ID 7. Substance $ Amount;
	datalines;
1004377 CO2     54
1004377 Methane 28
1004377 N2O     54
1004377 HFC     30
1000112 CO2     88
1000112 Methane 40
1000112 N2O     72
1000112 HFC     1
;

I'm sure there is an easy way to do this, but can't figure it out. Ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10
proc transpose data=have out=want (rename=(_name_=substance col1=amount));
    by id notsorted;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

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/

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

 


@JJ_83 wrote:

I have this wide dataset structure. 

data have;
	input ID 7. CO2 Methane N2O HFC;
	datalines;
1004377 54 28 54 30
1000112 88 40 72 1
;

And I want this long dataset structure. 

data want;
	input ID 7. Substance $ Amount;
	datalines;
1004377 CO2     54
1004377 Methane 28
1004377 N2O     54
1004377 HFC     30
1000112 CO2     88
1000112 Methane 40
1000112 N2O     72
1000112 HFC     1
;

I'm sure there is an easy way to do this, but can't figure it out. Ideas?


 

JJ_83
Obsidian | Level 7

Thank you!

mklangley
Lapis Lazuli | Level 10
proc transpose data=have out=want (rename=(_name_=substance col1=amount));
    by id notsorted;
run;
JJ_83
Obsidian | Level 7

I knew it was something simple. Thank you!

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!

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
  • 4 replies
  • 618 views
  • 4 likes
  • 3 in conversation