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

Hi,

 

I have the following dataset. I flagged the observation that has HbA1c <6.5 and if months is less or equal to 6 months.

 

patid hba1c months flag
1 6.6 0 0
1 6.1 3 1
1 7.9 5 0
1 6.3 3 1
2 6.6 0 0
2 6.4 4 1
2 6.4 3 1
3 6.4 0 1
3 8.8 6 0
3 5.3 3 1
3 5.5 4 1
3 3.2 6 1
3 7 8 0
3 7.5 3 0
3 7.6 5 0

 

However, when there is more than one consecutive flag (1), I want to keep the first Flag (1) as 1 (remission) and the following 1's as 0 (remission) until the next value is 0 (Highlighted red), by patid. 

 

Want:

patid hba1c months flag remission
1 6.6 0 0 0
1 6.1 3 1 1
1 7.9 5 0 0
1 6.3 3 1 1
2 6.6 0 0 0
2 6.4 4 1 1
2 6.4 3 1 0
3 6.4 0 1 1
3 8.8 6 0 0
3 5.3 3 1 1
3 5.5 4 1 0
3 3.2 6 1 0
3 7 8 0 0
3 7.5 3 0 0
3 7.6 5 0 0

 

Could anyone please help me with the code? I tried to search but I don't seem to get the right keyword for search. Thanks.

 

Sandyzman1

1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ
data want;
	set have;
	by patid;
		if hba1c < 6.5 and months <= 6 then flag = 1;
		remission = flag;
		if not first.patid and lag(flag) = 1 then remission = 0;
run;
patid hba1c months flag remission 
1 6.6 0 0 0 
1 6.1 3 1 1 
1 7.9 5 0 0 
1 6.3 3 1 1 
2 6.6 0 0 0 
2 6.4 4 1 1 
2 6.4 3 1 0 
3 6.4 0 1 1 
3 8.8 6 0 0 
3 5.3 3 1 1 
3 5.5 4 1 0 
3 3.2 6 1 0 
3 7.0 8 0 0 
3 7.5 3 0 0 
3 7.6 5 0 0 

View solution in original post

2 REPLIES 2
maguiremq
SAS Super FREQ
data want;
	set have;
	by patid;
		if hba1c < 6.5 and months <= 6 then flag = 1;
		remission = flag;
		if not first.patid and lag(flag) = 1 then remission = 0;
run;
patid hba1c months flag remission 
1 6.6 0 0 0 
1 6.1 3 1 1 
1 7.9 5 0 0 
1 6.3 3 1 1 
2 6.6 0 0 0 
2 6.4 4 1 1 
2 6.4 3 1 0 
3 6.4 0 1 1 
3 8.8 6 0 0 
3 5.3 3 1 1 
3 5.5 4 1 0 
3 3.2 6 1 0 
3 7.0 8 0 0 
3 7.5 3 0 0 
3 7.6 5 0 0 
sandyzman1
Obsidian | Level 7
Thanks, maguiremq!

Awesome, this works perfectly well. You are the best.

Sandyzman1

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