SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raddad34
Fluorite | Level 6

Hello, I have a dataset I'm trying to reshape using proc transpose but I cant quite seem to figure it out

 

 

data have;
input id $ record1 $ record2 $ record3 $;
datalines;
A1 a15 n14
A1 a15 b12 b17
A1 a12 b18
A2 a29 a28 a21
A3 a34 a36 c34;

I want this data to look like this: 

 

 

data want;
input id $ record $;A1 a15
A1 n14
A1 a15
A1 b12 
A1 b17
A1 a12
A1 b18
A2 a29
A2 a28 
A2 a21
A3 a34
A3 a36
A3 c34

Using proc transpose i get a set with multiple columns and i cant figure out how to get only one column. Does anyone have any tips on how to do this? Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
snoopy369
Barite | Level 11

You should just do this in the data step - easier and faster than coercing PROC TRANSPOSE to do what you want!  Just make an array of your variables and then `output` new rows when you have a value.

 


data want;
  set have;
  array recs record1-record3;
  do _i = 1 to dim(recs);
    if not missing(recs[_i]) then do;
    	record = recs[_i];
    	output;
    end;
  end;
  keep record id;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User
proc transpose data=have out=want prefix=record;
by ID;
var record1-record3;
run;

EDIT: In this case I agree with @snoopy369 a data step is easier with the duplicate entries, otherwise you need to add a grouping variable. 

 

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


@raddad34 wrote:

Hello, I have a dataset I'm trying to reshape using proc transpose but I cant quite seem to figure it out

 

 

data have;
input id $ record1 $ record2 $ record3 $;
datalines;
A1 a15 n14
A1 a15 b12 b17
A1 a12 b18
A2 a29 a28 a21
A3 a34 a36 c34;

I want this data to look like this: 

 

 

data want;
input id $ record $;A1 a15
A1 n14
A1 a15
A1 b12 
A1 b17
A1 a12
A1 b18
A2 a29
A2 a28 
A2 a21
A3 a34
A3 a36
A3 c34

Using proc transpose i get a set with multiple columns and i cant figure out how to get only one column. Does anyone have any tips on how to do this? Thank you.


 

Reeza
Super User

If you did want to use PROC TRANSPOSE I think you need to add a variable because your ID is repeated.

 

data temp;
set have;
by id;
if first.ID then counter=1;
else counter+1;
run;


proc transpose data=temp out=want prefix=record;
by ID counter;
var record1 record2 record3;
run;
snoopy369
Barite | Level 11

You should just do this in the data step - easier and faster than coercing PROC TRANSPOSE to do what you want!  Just make an array of your variables and then `output` new rows when you have a value.

 


data want;
  set have;
  array recs record1-record3;
  do _i = 1 to dim(recs);
    if not missing(recs[_i]) then do;
    	record = recs[_i];
    	output;
    end;
  end;
  keep record id;
run;
raddad34
Fluorite | Level 6
Thank you so much! this works perfectly!

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1892 views
  • 1 like
  • 3 in conversation