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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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