@andreas_lds
Thank you for your response. When I use the fix below, I am getting the correct logic but not the correct result:
![chandan_mishra_0-1644588046225.png chandan_mishra_0-1644588046225.png](https://communities.sas.com/t5/image/serverpage/image-id/68391i3D31C4DF28D562ED/image-size/medium?v=v2&px=400)
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.