Well, it took me a few different steps but I did come up with a solution:
data original;
length id 8 date 8 visittype $20;
infile cards dlm=',';
input ID dateval $ visittype $;
year=2017;
month=input(scan(dateval,1, '/'), 2.);
day=input(scan(dateval,2, '/'), 2.);
date = mdy(month,day, year);
format date mmddyy5.;
drop dateval year month day;
cards;
1, 2/1, ED
1, 2/2, hospitalization
1, 2/3, clinic A
1, 2/4, ED
1, 5/4, ED
1, 6/10, hospitalization
2, 2/1, ED
2, 2/5, ED
2, 2/10, clinic A
2, 2/13, hospitalization
2, 4/21, ED
;
run;
proc print data=original;
run;
proc sql;
create table firstdates as
select id, min(date) as fdate format = mmddyy5.
from original
where visittype='clinic A'
group by id;
create table firstdates2 as
select o.id, date, fdate, visittype,
case
when date <= fdate then 'Before'
when date > fdate then 'After'
end as BeforAft,
cat(calculated beforaft,visittype) as group
from original as o, firstdates as f
where o.id=f.id;
quit;
proc sql;
create table counts as
select id, group, count(*) as count
from firstdates2
where visittype ne 'clinic A'
group by id, group;
quit;
proc transpose data=counts out=c2(drop=_name_);
by id;
var count;
id group;
run;
proc print data=c2;
run;
... View more