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 !

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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