BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
km0927
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14
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;
Thanks,
Suryakiran

View solution in original post

2 REPLIES 2
SuryaKiran
Meteorite | Level 14
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;
Thanks,
Suryakiran
novinosrin
Tourmaline | Level 20
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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 969 views
  • 0 likes
  • 3 in conversation