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 1 0 2
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
if surgery_yn = 0 and had_surgery = 0 and admission_yn=0 then count+1;
if surgery_yn = 0 and had_surgery = 0 and admission_yn=0 then count+1;
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.