BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input patient_id week condition @@;
datalines;
1 1 0 
1 2 1 
1 3 3 
2 2 2 
2 5 1 
3 1 3 
4 2 0 
4 3 1 
4 4 1 
4 5 2 
;
run;

data want(drop = flag);
   set have;
   by patient_id;
   if first.patient_id then flag = 0;
   if flag = 0 then output;
   if condition = 1 then flag = 1;
   retain flag;
run;

 

Result:

 

patient_id week condition
1          1    0
1          2    1
2          2    2
2          5    1
3          1    3
4          2    0
4          3    1

 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input patient_id week condition @@;
datalines;
1 1 0 
1 2 1 
1 3 3 
2 2 2 
2 5 1 
3 1 3 
4 2 0 
4 3 1 
4 4 1 
4 5 2 
;
run;

data want(drop = flag);
   set have;
   by patient_id;
   if first.patient_id then flag = 0;
   if flag = 0 then output;
   if condition = 1 then flag = 1;
   retain flag;
run;

 

Result:

 

patient_id week condition
1          1    0
1          2    1
2          2    2
2          5    1
3          1    3
4          2    0
4          3    1

 

Quentin
Super User

Nothing is easy. : )

 

Can you show the code you have tried?  Did it involve RETAIN?  That will help others help you.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 529 views
  • 1 like
  • 3 in conversation