BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shasank
Quartz | Level 8

Hi Community,

 

Thank you for your time.

I am stuck with a situation where I have to flatten a dataset with suffix from different variable.

 

Here is the raw dataset. 

idIntakectxszpel
22111000
22114001
22115000
33221000
33224000
33225000
44221000
55331001
55334000
55335000

 

The idea is to use the intake variable and add it as a suffix and flatten the dataset to get one row per patient final file.

Expected Output. 

id ctx1ctx4ctx5sz1sz4sz5pel1pel4pel5
2211 000000010
3322 000000000
4422 0..0..0..
5533 000000100

 

I tried to use proc transpose by ID but had not luck.

 

Any help is appreciated. Thank you for looking. 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input id	Intake	ctx	sz	pel;
cards;
2211	1	0	0	0
2211	4	0	0	1
2211	5	0	0	0
3322	1	0	0	0
3322	4	0	0	0
3322	5	0	0	0
4422	1	0	0	0
5533	1	0	0	1
5533	4	0	0	0
5533	5	0	0	0
;

proc transpose data=have out=temp;
by id intake;
var 	ctx	sz	pel;
run;
proc transpose data=temp out=want(drop=_name_);
by id ;
var col1;
id _name_ intake;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

data have;
input id	Intake	ctx	sz	pel;
cards;
2211	1	0	0	0
2211	4	0	0	1
2211	5	0	0	0
3322	1	0	0	0
3322	4	0	0	0
3322	5	0	0	0
4422	1	0	0	0
5533	1	0	0	1
5533	4	0	0	0
5533	5	0	0	0
;

proc transpose data=have out=temp;
by id intake;
var 	ctx	sz	pel;
run;
proc transpose data=temp out=want(drop=_name_);
by id ;
var col1;
id _name_ intake;
run;
novinosrin
Tourmaline | Level 20

In case you wanted the order exactly like you showed-->

 


data have;
input id	Intake	ctx	sz	pel;
cards;
2211	1	0	0	0
2211	4	0	0	1
2211	5	0	0	0
3322	1	0	0	0
3322	4	0	0	0
3322	5	0	0	0
4422	1	0	0	0
5533	1	0	0	1
5533	4	0	0	0
5533	5	0	0	0
;

data temp;
 set have;
 array t ctx	sz	pel;
 length vn $32;
 do over t;
  v=t;
  vn=vname(t);
  grp=_i_;
  output;
 end;
 drop ctx	sz	pel;
run;
proc sort data=temp;
by id grp;
run;
proc transpose data=temp out=want(drop=_name_);;
by id ;
id vn intake;
var v;
run;
shasank
Quartz | Level 8
This is really helpful. Thank you.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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