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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.