BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

What is the way to create wanted data set from have data set?

This is transpose of one row (wide) into long with putting var labels into one column in the desired data set

Data have;
label X1='abc' X2='frg' X3='Per' X4='Tim'  X5='Loc';
input X1 X2 X3 X4 X5;
cards;
100 200 50 300 180
;
Run;


Data wanted;
input field_name $ field_label $ value;
cards;
X1 abc 100
X2 frg 200
X3 per 50
X4 Tim 300
X5 Loc 180
;
Run;
2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data want;
   set have;
   array x x:;
   do over x;
      field_name = vname(x);
	  field_label = vlabel(x);
	  value = x;
	  output;
   end;
   keep field_name field_label value;
run;

 

Result:

 

field_name field_label value 
X1         abc         100 
X2         frg         200 
X3         Per         50 
X4         Tim         300 
X5         Loc         180 
andreas_lds
Jade | Level 19

Proc transpose can do this:

proc transpose data=have out=want(rename=(col1 = value)) label=field_label name=field_name;
   var x1-x5;
run;

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!

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