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:
Thanks
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;
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;
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:
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;
Thanks !
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.