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

I have the following dataset 

 

IDGroupAgeGender
1161Female
1261Female
2262Male
3164Female
4166Male
4266Male
5264Male
6167Female
6267Female

 

What I would like to do is that if same ID has 'Group = 1 and Group = 2" then I want to create a new variable for ID: final_group = Group 1 if Group = 1; final_group = 'Group 2 Indirect';

if only one record then use same final_group = Group

 

Something like this:

IDGroupFinal_phaseAgeGender
11Group 161Female
12Group 2 Indirect61Female
22Group 2 Direct62Male
31Group 164Female
41Group 166Male
42Group 2 Indirect66Male
52Group 2 Direct64Male
61Group 167Female
62Group 2 Indirect67Female

 

I tried to do max per patient and create additional datasets but was wondering if there is an easy way

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ
data have ;
	infile cards ;
	input id  group  age gender $ ;
cards ;
1	1	61	Female
1	2	61	Female
2	2	62	Male
3	1	64	Female
4	1	66	Male
4	2	66	Male
5	2	64	Male
6	1	67	Female
6	2	67	Female
;
run ;

proc sort data=have out=srtdhave ;
	by id group ;
run ;

data want ;
	retain group1Flag 0 ;
	drop group1Flag ;
	length final_phase $20 ;
	set srtdhave ;
	by id group ;
	if first.id then do ;
		group1Flag=0 ;
	end ;
	if group=1 then 
		group1Flag=1 ;
	final_phase="Group "!!left(putn(group,"8.")) ;
	if group=2 then do ;
		if group1Flag=1 then do ;
			final_phase=trim(final_phase)!!" Indirect" ;
		end ;
		else
		do ;
			final_phase=trim(final_phase)!!" Direct" ;
		end ;
	end ;
run ;

First and Last are your friend 

View solution in original post

1 REPLY 1
AMSAS
SAS Super FREQ
data have ;
	infile cards ;
	input id  group  age gender $ ;
cards ;
1	1	61	Female
1	2	61	Female
2	2	62	Male
3	1	64	Female
4	1	66	Male
4	2	66	Male
5	2	64	Male
6	1	67	Female
6	2	67	Female
;
run ;

proc sort data=have out=srtdhave ;
	by id group ;
run ;

data want ;
	retain group1Flag 0 ;
	drop group1Flag ;
	length final_phase $20 ;
	set srtdhave ;
	by id group ;
	if first.id then do ;
		group1Flag=0 ;
	end ;
	if group=1 then 
		group1Flag=1 ;
	final_phase="Group "!!left(putn(group,"8.")) ;
	if group=2 then do ;
		if group1Flag=1 then do ;
			final_phase=trim(final_phase)!!" Indirect" ;
		end ;
		else
		do ;
			final_phase=trim(final_phase)!!" Direct" ;
		end ;
	end ;
run ;

First and Last are your friend 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 800 views
  • 0 likes
  • 2 in conversation