Obsidian | Level 7

## Use Rank in SAS

```if first.patid then call missing(difference);
if difference = . or difference <= 180 then ep = 0;
ep + 1;```

Here is the sample data:

PatID.        FirstDayofService.         LastDayofService.  Difference Episode

 AB1 2015-10-31 2015-10-31 . 1 AB1 2016-05-14 2016-05-14 196 2 AB1 2017-12-13 2017-12-13 578 3 AB1 2017-12-13 2017-12-13 0 1 AB1 2017-12-13 2017-12-13 0 1

In this sample data, I am calculating the difference between first day of service and next first day of service. If the difference is 0 or < 180 days then I am trying to add the same episode. Else Episode will increase by 1. For the first three rows highlighted in green, its working correctly. For the next two rows, highlighted in orange, it's not working correctly as its incorrectly showing episode as 1. It should take the episode based on previous episode value. i.e. 3 if the difference is less than 180. In this case the correct response for last two rows in episode column should be 3.

1 ACCEPTED SOLUTION

Accepted Solutions
Obsidian | Level 7

## Re: Use Rank in SAS

Thank you for your response. When I use the fix below, I am getting the correct logic but not the correct result:

Episode should show 1,2,3,3,3, Here it is starting at 0.

I tried changing the initial value of episode to 1,

```rsubmit;
data check1;
set check;
by FDS;/*First day of Service*/
if first.patid then do;
difference = 0;
episode = 1;
end;

if difference > 180 then episode +1;
run;
endrsubmit;```

But even then its showing the same results.

3 REPLIES 3

## Re: Use Rank in SAS

If the difference is 0 or < 180 days then I am trying to add the same episode.

Right know your code does the opposite:

`if difference = . or difference <= 180 then ep = 0;`

A fix could be:

``````if first.patid then do;
difference = 0;
ep = 0;
end;

if difference > 180 then ep +1;

``````
Obsidian | Level 7

## Re: Use Rank in SAS

Thank you for your response. When I use the fix below, I am getting the correct logic but not the correct result:

Episode should show 1,2,3,3,3, Here it is starting at 0.

I tried changing the initial value of episode to 1,

```rsubmit;
data check1;
set check;
by FDS;/*First day of Service*/
if first.patid then do;
difference = 0;
episode = 1;
end;

if difference > 180 then episode +1;
run;
endrsubmit;```

But even then its showing the same results.

Obsidian | Level 7

## Re: Use Rank in SAS

Sorry skip this previous message. Figured it out. Didnt add proc sort by patid and fds.

rsubmit;
out=check;
by PATID fds;
run;
endrsubmit;

rsubmit;
data check1;
set check;
by PATID FDS;
if first.PatID then do;
difference = 0;
episode = 1;
end;

if difference > 180 then episode +1;
run;
endrsubmit;

Its working now.
Discussion stats
• 3 replies
• 497 views
• 1 like
• 2 in conversation