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 visiting number(except admission) before they got surgery.

 

raw data is here.

 

id date admission_YN surgery_YN

1 2002/01/28 0 0
1 2002/02/15 0 0
1 2002/04/06 1 0

1 2002/05/05 0 0
1 2002/06/07 1 1
1 2002/08/10 0 0
1 2003/01/12 0 0
2 2003/01/15 0 0
2 2003/02/10 1 0
2 2003/03/10  1 1
2 2003/04/08 0 0

 

and with this code, i made a data set. but i can't exclude(=jump over) visiting for admission.

 

proc sort data=have;
by id date;
run;
data want;
retain had_surgery;
set have;
by Id date;
if first.id then
do;
had_surgery = 0;
count = 0;
end;
if surgery_yn = 0 and had_surgery = 0 then count+1;
else if surgery_yn = 1 then
do;
had_surgery = 1;
count=0 ;
end;
run;

 

id date admission_YN surgery_YN count

1 2002/01/28 0 0 1
1 2002/02/15 0 0 2
1 2002/04/06 1 0 3

1 2002/05/05 0 0 4
1 2002/06/07 1 1 0
1 2002/08/10 0 0 0
1 2003/01/12 0 0 0
2 2003/01/15 0 0 1
2 2003/02/10 1 0 2
2 2003/03/10  1 1 0
2 2003/04/08 0 0 0

 

i want make data set like this. how can i jump over admission event?

 

id date admission_YN surgery_YN count

1 2002/01/28 0 0 1
1 2002/02/15 0 0 2
1 2002/04/06 12

1 2002/05/05 0 0 3
1 2002/06/07 1 1 0
1 2002/08/10 0 0 0
1 2003/01/12 0 0 0
2 2003/01/15 0 0 1
2 2003/02/10 1 0 1
2 2003/03/10 1 1 0
2 2003/04/08 0 0 0

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
if surgery_yn = 0 and had_surgery = 0 and admission_yn=0 then count+1;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
if surgery_yn = 0 and had_surgery = 0 and admission_yn=0 then count+1;
--
Paige Miller
novinosrin
Tourmaline | Level 20
data have;
input id date : yymmdd10. surgery_YN;
format date yymmdd10.;
cards;
1 2002/01/28 0 0
1 2002/02/15 0 0
1 2002/04/06 1 0
1 2002/05/05 0 0
1 2002/06/07 0 1
1 2002/08/10 0 0
1 2003/01/12 0 0
2 2003/01/15 0 0
2 2003/02/10 1 0
2 2003/03/10 0 1
2 2003/04/08 0 0
;





data want;
set have;
by id date;
if first.id then do;f=0;count=0;end;
f+surgery_YN;
if f<1 then count+1;
if lag(surgery_YN)=1 and surgery_YN=0 then count=0;
drop f;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

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
  • 1449 views
  • 0 likes
  • 3 in conversation