DATA Step, Macro, Functions and more

How to flag rows?

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

How to flag rows?

Hello,

Untitled.png

In the data the date difference is the differene between the row's date to the previous date.  I want to flag patients where there is difference of  >= 120 days and mark all the dates after that >120 days. If there is a patients who have 2 rows of >120 days then I want to mark first  >= 120 days to the 2nd >120 days as "R1" and "R2" for dates greater than 2nd >120 days.

 

Thanks!


Accepted Solutions
Solution
‎06-28-2017 10:09 AM
Super User
Posts: 5,497

Re: How to flag rows?

Posted in reply to Sujithpeta

I think this is what you are asking for:

 

data want;

set have;

by patient_ID;

if first.patient_ID then count=0;

drop count;

if difference_between_dates >= 120 then count + 1;

if count > 0 then recurrence = 'R' || put(count, z2.);

run;

 

I did add a zero in there, using the Z2. format.  If you are sure that you will never require double digits to count the number of recurrences, you could change that to the 1. format instead.

View solution in original post


All Replies
Super User
Posts: 7,760

Re: How to flag rows?

Posted in reply to Sujithpeta

Since I won't type off a picture, here's an example with simplified data:

data have;
input patient $ date;
cards;
X 1
X 6
X 130
X 135
X 140
X 350
X 355
;
run;

data want;
set have;
by patient;
retain recurr;
diff = date - lag(date);
if first.patient
then do;
  recurr = "  ";
  diff = 0;
end;
if diff > 120
then do;
  if recurr = 'R1'
  then recurr = 'R2';
  else recurr = 'R1';
end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎06-28-2017 10:09 AM
Super User
Posts: 5,497

Re: How to flag rows?

Posted in reply to Sujithpeta

I think this is what you are asking for:

 

data want;

set have;

by patient_ID;

if first.patient_ID then count=0;

drop count;

if difference_between_dates >= 120 then count + 1;

if count > 0 then recurrence = 'R' || put(count, z2.);

run;

 

I did add a zero in there, using the Z2. format.  If you are sure that you will never require double digits to count the number of recurrences, you could change that to the 1. format instead.

Contributor
Posts: 43

Re: How to flag rows?

Posted in reply to Astounding

Thank you, it worked!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 128 views
  • 1 like
  • 3 in conversation