BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

I have this data set that I want to transform so that it's one row per ID. The problem is that there are multiple values for the an ID variable.

 

Here is the data set in question:

data have1;
infile datalines dsd dlm=",";
	input id $ category $ val $;
datalines;
001, color, blue
001, color, red
002, color, orange
003, color, red
003, color, orange,
003, color, blue
004, color, aqua
005, color, purple
; 
run;

Ideally, I would want it so it looks like:

id      category val1       val2         val3

001  color       blue       red

002  color      orange

003  color      red          orange     blue

004  color     aqua

005  color     purple

 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

PROC TRANSPOSE can handle that example very easily.  Perhaps you over simplified your example?

data have1;
	input id $ category $ val $;
datalines;
001  color  blue
001  color  red
002  color  orange
003  color  red
003  color  orange 
003  color  blue
004  color  aqua
005  color  purple
; 

proc transpose data=have1 out=want1(drop=_name_) prefix=VAL ;
  by id category ;
  var val;
run;

Result

Obs    id     category    VAL1      VAL2      VAL3

 1     001     color      blue      red
 2     002     color      orange
 3     003     color      red       orange    blue
 4     004     color      aqua
 5     005     color      purple

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

PROC TRANSPOSE can handle that example very easily.  Perhaps you over simplified your example?

data have1;
	input id $ category $ val $;
datalines;
001  color  blue
001  color  red
002  color  orange
003  color  red
003  color  orange 
003  color  blue
004  color  aqua
005  color  purple
; 

proc transpose data=have1 out=want1(drop=_name_) prefix=VAL ;
  by id category ;
  var val;
run;

Result

Obs    id     category    VAL1      VAL2      VAL3

 1     001     color      blue      red
 2     002     color      orange
 3     003     color      red       orange    blue
 4     004     color      aqua
 5     005     color      purple
ballardw
Super User

For your given example:

 

proc transpose data=have1 out=trans (drop=_name_)
  prefix=val;
   by id category;
   var val;
run;

Transpose uses the BY variables to create groupings.

The default behavior is to use the Prefix (defaults to col) as the name of the transposed variables with an increment for number of values in the by group.

 

If you were concerned with the ID statement that is for a variable used to NAME output variables and generally doesn't allow multiple values per By group.

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