BookmarkSubscribeRSS Feed
Lav001
Fluorite | Level 6

Hi All,

 

Could you let me know how to achieve for below logic:-

 

I need to flag the below records from table 1 in such a way that, I need to get the second table Flag column populated.

 

Data is sorted by column 1,2.

 

       Scenario : should be flagged as 1 for first visit

                         and for remaining visits we need to --> compare the current visit with the previous visit based on the Reason column. If records have same                                                                                             reason, then compare to prior visit Reason (Until 3 times, only if Reason value is same).

                         Please see the logic column in the Desired outcome table.

 

Patient_IDVisitReasonFlag
5001Heart 
5002Kidney 
5003Lungs 
5004Lungs 
5005Lungs 

 

Desired Outcome:

 

Patient_IDVisitReasonFlagLogic
5001Heart1since first visit we will flag as 1
5002Kidney1comparing current visit (Kidney) with previous (Heart), flagging as 1 when different value
5003Lungs1comparing current visit (Lungs) with previous (Kidney), flagging as 1 when different value
5004Lungs0comparing current visit (Lungs) with previous (Lungs), when same value, compare with prior value  which is (Kidney)(2nd visit), since it is different we will assign 0 
5005Lungs1comparing current visit (Lungs) with previous (Lungs), when same value, compare with prior value  which is (Lungs)(3rd visit), since it is same we will assign 1 

 

 Note:- we need to compare only until last 3 visits based on Reason column.

 

Thanks

Lavanya

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

 

Something like this?

 

 

FLAG = ifn(PATIENT_ID ne lag(PATIENT_ID)                         ,  1
     , ifn(PATIENT_ID = lag1(PATIENT_ID) & REASON ne lag1(REASON),  1
     , ifn(PATIENT_ID = lag2(PATIENT_ID) & REASON ne lag2(REASON),  0
     , ifn(PATIENT_ID = lag3(PATIENT_ID) & REASON ne lag3(REASON),  1
     ,                                                              0 ))));

 

mkeintz
PROC Star

What is your rule for visit 2, if both visit 1 and visit 2 are HEART?  This is a case where a given visit is the same as the prior visit, but there in no earlier visit to use as the criterion.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mkeintz
PROC Star

Putting aside my question about what to do if visit1=visit2, your logic can be restated as. 

   The flag always=1 EXCEPT when the current reason is the same as both prior reasons, in which case it is zero.

So just defaulting to 1 and looking for the special condition for zero is tempting:

 

data want;
  set have;

  flag=1;
  if (reason=lag(reason)=lag2(reason)) and (patient_id=lag2(patient_id)) then flag=0;
run;

 

or, using syntax that might be slightly more general than  "reason=lag(reason)=lag2(reason)", you could used:

  if (reason=lag(reason)) and (reason=lag2(reason)) and (patient_id=lag2(patient_id)) then flag=0;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
andreas_lds
Jade | Level 19

I would not use lag at all, but retain the last two reasons.

data want;
   set have;
   by Patient_ID;
   
   length Flag 8 _r1 _r2 $ 8; /* Change the length of _r1 and _r2 to match with the length of "reason" */
   retain _r1 _r2;
   drop _r1 _r2;
   
   if first.Patient_ID then do;
      Flag = 1;
      call missing(_r1, _r2);
   end;
   else do;
      Flag = (Reason ^= _r1 or Reason = _r1 and Reason = _r2);
   end;
   
   _r2 = _r1;
   _r1 = Reason;
run;
ChrisNZ
Tourmaline | Level 20

> I would not use lag at all, but retain the last two reasons.
Yep, two possible methods.
I find mine more legible.

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
  • 5 replies
  • 2307 views
  • 0 likes
  • 4 in conversation