DATA TABLEA;
input Name $1. ID $5. smoker_code Year;
Cards;
A ID1 0 2012
B ID2 1 2010
;
Run;
DATA TABLEB;
input Name $1. ID $5. want Year;
Cards;
A ID1 0.5 2010
A ID1 0.6 2011
A ID1 0.7 2012
B ID2 0.8 2008
B ID2 0.8 2009
B ID2 0.8 2010
B ID2 0.9 2011
;
Run;
Proc sql;
Create table ABC_SQL_full as
select A.*
,B.want as want1
,B.year as Year_past
,C.want as Want2
from (select *,MONOTONIC() as ORD from TABLEA) as a
full join TABLEB as b
on A.ID=B.ID
and A.Name=B.name
left join TABLEB as c
on A.ID=c.ID
and a.Name=c.name
and b.year=c.year
where b.year le a.year
order by ORD,year_past;
quit;
/* how to perform the same SQL join results with HASH method
1.HASH full join with tableB
2.and HASH left join with same tableB with condition b.year=c.year in the defined key
3.and having the same where and order condition*/
/* here it's an left join in hash that i can use for the second join ... */
data ABC_HASH_full;
if 0 then set TABLEB ;
set TABLEA ;
if _n_=1 then
do;
declare hash hhh(dataset: "TABLEB", multidata:'y');
hhh.DefineKey('ID','Name');
hhh.DefineData('want');
hhh.DefineDone();
end;
if hhh.find(key:ID,Key:Name)=0 then do;
output ABC_SQL_full;
end;
run;
See if you can use this as a template
data hash_full;
if _N_ = 1 then do;
dcl hash h(dataset : "TABLEB(rename=Year=Year_Past", multidata : "Y");
h.definekey("Name", "ID");
h.definedata("want", "Year_Past");
h.definedone();
end;
set TABLEA;
Year_Past = .;want = .;
do while (h.do_over() = 0);
if Year_Past <= Year then output;
end;
run;
See if you can use this as a template
data hash_full;
if _N_ = 1 then do;
dcl hash h(dataset : "TABLEB(rename=Year=Year_Past", multidata : "Y");
h.definekey("Name", "ID");
h.definedata("want", "Year_Past");
h.definedone();
end;
set TABLEA;
Year_Past = .;want = .;
do while (h.do_over() = 0);
if Year_Past <= Year then output;
end;
run;
The Set Statement read TableA sequentially, so the order from TableA will be intact in the output data set 🙂
@bebess is this the answer you're after?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.