BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new 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!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

6 REPLIES 6
Patrick
Opal | Level 21

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)

CharlotteCain
Quartz | Level 8

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?

Patrick
Opal | Level 21

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)

CharlotteCain
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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