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

Hi,

From this table:

DATA HAVE;
INPUT CODE TYPE$ CATEGORY$;
CARDS;

15 A Y6D
15 B K45
08 A LS7
08 B G27
63 A A09
63 A Q98
63 B Z98
05 A P09
10 A W87
;
RUN;

I would like to obtain the following result:

WANT.jpg

 

Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

Indeed

 

DATA HAVE;
INPUT CODE TYPE$ CATEGORY$ COLOR$;
CARDS;
15 A Y6D BLUE
15 B K45 YELLOW
08 A LS7 RED
08 B G27 RED 
63 A A09 BLACK
63 A Q98 WHITE
63 B Z98 GRAY
05 A P09 GREEN
10 A W87 GRAY
;
   RUN;
proc sort;
   by code type;
   run;

data haveidx;
   set have;
   by code type;
   if first.type then i=0;
   i + 1;
   run;
proc transpose data=haveidx out=tall name=vname;
   by code type i;
   var category color;
   run;

proc transpose data=tall out=want delim=_;
   by code;
   var col1;
   id vname type i;
   run;

data_null___0-1714758921683.png

 

View solution in original post

4 REPLIES 4
data_null__
Jade | Level 19

You should sort but it does work notsorted.

 

DATA HAVE;
INPUT CODE TYPE$ CATEGORY$;
CARDS;
15 A Y6D
15 B K45
08 A LS7
08 B G27
63 A A09
63 A Q98
63 B Z98
05 A P09
10 A W87
;
   RUN;

data haveidx;
   set have;
   by code type notsorted;
   if first.type then i=0;
   i + 1;
   run;
proc transpose data=haveidx out=want prefix=Type_ delim=_;
   by code notsorted;
   var category;
   id type i;
   run;

Capture.PNG

sasuser_8
Obsidian | Level 7

If I have two variables to transpose instead of one, can I still do it with a transpose ? By example:

DATA HAVE;
INPUT CODE TYPE$ CATEGORY$ COLOR$;
CARDS;
15 A Y6D BLUE
15 B K45 YELLOW
08 A LS7 RED
08 B G27 RED 
63 A A09 BLACK
63 A Q98 WHITE
63 B Z98 GRAY
05 A P09 GREEN
10 A W87 GRAY
;
   RUN;

For this result:

 

WANT_2.jpg

data_null__
Jade | Level 19

Indeed

 

DATA HAVE;
INPUT CODE TYPE$ CATEGORY$ COLOR$;
CARDS;
15 A Y6D BLUE
15 B K45 YELLOW
08 A LS7 RED
08 B G27 RED 
63 A A09 BLACK
63 A Q98 WHITE
63 B Z98 GRAY
05 A P09 GREEN
10 A W87 GRAY
;
   RUN;
proc sort;
   by code type;
   run;

data haveidx;
   set have;
   by code type;
   if first.type then i=0;
   i + 1;
   run;
proc transpose data=haveidx out=tall name=vname;
   by code type i;
   var category color;
   run;

proc transpose data=tall out=want delim=_;
   by code;
   var col1;
   id vname type i;
   run;

data_null___0-1714758921683.png

 

sasuser_8
Obsidian | Level 7

Thanks !

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
  • 268 views
  • 1 like
  • 2 in conversation