Solved
Contributor
Posts: 58

# How to flag rows?

Hello,

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: 6,751

## Re: How to flag rows?

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.

All Replies
Super User
Posts: 10,209

## Re: How to flag rows?

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
How to convert datasets to data steps
How to post code
Solution
‎06-28-2017 10:09 AM
Super User
Posts: 6,751

## Re: How to flag rows?

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: 58