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: Rick Wicklin presents Ten Tips for Effective Statistical Graphics (with SAS code) on Wednesday March 26.
Register now at https://www.basug.org/events.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

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
  • 697 views
  • 1 like
  • 3 in conversation