hello. i made data set, counting patient's surgery event.
raw data is here. this is patient's billing data. it is ordered by date.
id surgery_YN
1 0
1 0
1 0
1 0
1 1
1 0
1 0
1 1
1 1
1 0
2 0
2 0
2 1
2 0
2 1
2 0
2 0
and i want to count surgery event in cumulative sum and make other count zero. how can i jump over non-surgery event?
id surgery_YN count
1 0 0
1 0 0
1 0 0
1 0 0
1 1 1
1 0 0
1 0 0
1 1 2
1 1 3
1 0 0
2 0 0
2 0 0
2 1 1
2 0 0
2 1 2
2 0 0
2 0 0
data have;
input id surgery_YN;
datalines;
1 0
1 0
1 0
1 0
1 1
1 0
1 0
1 1
1 1
1 0
2 0
2 0
2 1
2 0
2 1
2 0
2 0
;
run;
proc sort data=have;
by id;
data want(drop=count_pre);
retain count;
set have;
by id;
if first.id then count_pre=0;
if surgery_YN^=0 then count_pre+1;
if surgery_YN=0 then count=0;
else count=count_pre;
run;
data have;
input id surgery_YN;
datalines;
1 0
1 0
1 0
1 0
1 1
1 0
1 0
1 1
1 1
1 0
2 0
2 0
2 1
2 0
2 1
2 0
2 0
;
run;
proc sort data=have;
by id;
data want(drop=count_pre);
retain count;
set have;
by id;
if first.id then count_pre=0;
if surgery_YN^=0 then count_pre+1;
if surgery_YN=0 then count=0;
else count=count_pre;
run;
data have;
input id surgery_YN;
datalines;
1 0
1 0
1 0
1 0
1 1
1 0
1 0
1 1
1 1
1 0
2 0
2 0
2 1
2 0
2 1
2 0
2 0
;
run;
data want;
set have;
by id;
if first.id then _iorc_=0;
count=surgery_YN;
if surgery_YN then do; _iorc_+surgery_YN;count=_iorc_;end;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.