%let example=Gender Age;
proc sql;
create table want as
select *
from table1 as t1
inner join table2 as t2
/* i want to use &example in order to define dynamically the key of my join*/
on t1.gender=t2.gender
and t1.Age=t2.Age
/*******/
;
quit;
Yes, as a start:
%do i=1 to ...
%let tmp=%scan(&example.,&i);
on t1.&tmp.=t2.&tmp.
%end;
%end;
...
Really not worth doing though as risky, even if you have done loads of pre-checks, the logic of the join based on your data may not be what is wanted.
Not easily, you would need macro code to loop over each word in the list, then generate out the on part of it. I would question why? If your rules are strict enough to allow a join like that, it is strict enough to just write the join and not need a macro list. Otherwise what happens if the variable isn't in both, if its different types, if the join is correct for the data etc. Far more problems will be created than solved by this.
Yes, as a start:
%do i=1 to ...
%let tmp=%scan(&example.,&i);
on t1.&tmp.=t2.&tmp.
%end;
%end;
...
Really not worth doing though as risky, even if you have done loads of pre-checks, the logic of the join based on your data may not be what is wanted.
Note that a join like this is bad programming practice, as it will unvariably throw a WARNING for a variable that already exists.
It is much easier if you just use SAS code instead of SQL code.
%let example=Gender Age;
data want ;
merge table1 (in=in1) table2 (in=in2);
by &example;
if in1 and in2;
run;
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.