A Macro parameterisation question?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 137
Accepted Solution

A Macro parameterisation question?

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!


Accepted Solutions
Solution
‎07-14-2015 07:01 AM
Respected Advisor
Posts: 4,130

Re: A Macro parameterisation question?

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)

View solution in original post


All Replies
Respected Advisor
Posts: 4,130

Re: A Macro parameterisation question?

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)

Frequent Contributor
Posts: 137

Re: A Macro parameterisation question?

Hi Patrick, I hope you are well. Sun is shining down under? Thank you.I like that and one sweet comment "cheeky" Smiley Happy 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?

Solution
‎07-14-2015 07:01 AM
Respected Advisor
Posts: 4,130

Re: A Macro parameterisation question?

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)

Frequent Contributor
Posts: 137

Re: A Macro parameterisation question?

Fabulous Patrick, Thank you so much. Have a great evening! Cheers

Super User
Super User
Posts: 7,686

Re: A Macro parameterisation question?

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).

Super User
Super User
Posts: 6,842

Re: A Macro parameterisation question?

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 239 views
  • 3 likes
  • 4 in conversation