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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1106 views
  • 0 likes
  • 2 in conversation