How do write the macro syntax and logic to parameterise properly for the following need?
%macro exec(dsn);
data want1;
set &dsn;
run;
%mend;
%exec(input_dataset1)
%exec(input_dataset2)
When &dsn resolves to input_dataset1 the program creates want1, and my need is then &dsn resolves to input_dataset2 in the second call execution, i want the output dataset as want2. Please advice and help?
Thanks!
Sun is shining but it's cold. Can't wait that Winter is over! Two more months to go.
Here a code variant which lets you choose the name.
%macro exec(dsn,dsn_out);
%if &dsn_out= %then %let dsn_out=want;
data;
set &dsn;
run;
proc datasets lib=work nolist;
change %scan(&syslast,-1)=&dsn_out._%sysfunc(compress(%scan(&syslast,-1),,kd));
run;
quit;
%mend;
%exec(sashelp.class)
%exec(sashelp.class,test)
If it doesn't need to be "want" but just data set names with consecutive numbering then you could leave it to SAS using code as below. The resulting data sets would then be called "data1", "data2" and so on.... Would this be sufficient for your purposes?
%macro exec(dsn);
data;
set &dsn;
run;
%mend;
%exec(sashelp.class)
%exec(sashelp.class)
Hi Patrick, I hope you are well. Sun is shining down under? Thank you.I like that and one sweet comment "cheeky" Well, that will do for my testing purposes. Do you mind helping me with a resolve that will give me the choice to name the datasets?
Sun is shining but it's cold. Can't wait that Winter is over! Two more months to go.
Here a code variant which lets you choose the name.
%macro exec(dsn,dsn_out);
%if &dsn_out= %then %let dsn_out=want;
data;
set &dsn;
run;
proc datasets lib=work nolist;
change %scan(&syslast,-1)=&dsn_out._%sysfunc(compress(%scan(&syslast,-1),,kd));
run;
quit;
%mend;
%exec(sashelp.class)
%exec(sashelp.class,test)
Fabulous Patrick, Thank you so much. Have a great evening! Cheers
If the informationyou have provided matches, then just drop the naming and use the number:
%macro exec(dsn);
data want&dsn.;
set input_dataset&dsn.;
run;
%mend exec;
%exec(dsn=1);
%exec(dsn=2);
There may however be easier ways to achieve what you want depending on what it is your doing. For example, setting your data together, then doing processing, then splitting again:
data inter;
set input_dataset1 (in=a)
input_dataset2 (in=b);
if a then file=1;
if b then file=2;
run;
/* do processing here */
data want1 want2;
set inter;
if file=1 then output want1;
else output want2;
run;
Or maybe you don't need to split them at all (which is generally my preferred option).
If you really want the macro to remember the number of times it has been called then you will need to use a global macro variable.
%macro exec(dsn);
%global exec_cnt ;
%let exec_cnt=%eval(&exec_cnt+1);
data want&exec_cnt;
set &dsn;
run;
%mend;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.