Hi @jomag
Im sorry to say, but you have choosen a overly complicated approach. SAS Macro code is not intended for processing of observations in a data set, and you can get your wanted result in a much simpler way. Here is one example:
* Note: last id changed from 2,02 to 2,01 to match values in supplied code and other data set;
data A;
informat Studyidnew $4. Beginday 8. Endday 8.;
input Studyidnew Beginday Endday;
cards;
1,01 0 158
1,01 159 180
1,01 181 1387
1,01 1388 1388
2,01 0 258
2,01 259 261
2,01 262 292
;
run;
data B;
informat Studyidnew $4. endpoint_type $5. endpoint_date 8.;
input Studyidnew endpoint_type endpoint_date;
cards;
1,01 ED 131
1,01 HOSP 132
1,01 HOSP 1338
2,01 TRANS 152
2,01 ED 261
2,01 HOSP 264
;
run;
proc sql;
create table w1 as
select
a.Studyidnew,
a.Beginday,
a.Endday,
ifn(b.endpoint_type = 'ED',1,0) as ED,
ifn(b.endpoint_type = 'HOSP',1,0) as HOSP,
ifn(b.endpoint_type = 'DEATH',1,0) as DEATH,
ifn(b.endpoint_type = 'TRANS',1,0) as TRANS ,
b.endpoint_date
from A left join B
on
a.Studyidnew = b.Studyidnew
and a.Beginday <= b.endpoint_date
and a.Endday >= b.endpoint_date
order by
a.Studyidnew,
a.Beginday;
quit;
proc sql;
create table want as
select distinct
Studyidnew,
Beginday,
Endday,
sum(ED) as ED,
sum(HOSP) as HOSP,
sum(DEATH) as DEATH,
sum(TRANS) as TRANS
from w1
group by
Studyidnew,
Beginday;
quit;
... View more