DATA Step, Macro, Functions and more

Macro variable inside call execute

Reply
Contributor
Posts: 45

Macro variable inside call execute

[ Edited ]

Hi all,

 

At the end of a program I want to save a table from the WORK library to another library.The table name can also change.

 

Then I created the macro SAVING:

 

%macro SAVING(
	library, 
	input_table,
	output_table);

PROC COPY 
inlib=WORK outlib= &library.;
SELECT &input_table.;
run;

%if %sysfunc(exist(&library..&output_table.)) %then %do;
	PROC DELETE DATA= &library..&output_table.;
%end;

PROC DATASETS NOPRINT library=&library.;
    change &input_table.= &output_table.;
run;
quit;
%mend;

My table name is store in a macrovariable. named outname.

I want to call the macro with the macrovariable name.

data _null_;

call execute('%SAVING(
					library = OUT_LIB,
					input_table = FINAL_TABLE,
					output_table = &outname.)');
run;

But when I do this I have an issue in front of the "change" row of the macro

73: LINE and COLUMN cannot be determined.
ERROR 73-322: Expecting an =.

Do you know what is wrong?

Thanks,

Super User
Super User
Posts: 9,227

Re: Macro variable inside call execute

[ Edited ]

You need to put double quotes around the text to invoke macro variables:

data _null_;
 call execute(cats('%saving(library=out_lib,input_table=final_table,output_table=',"&outname.".');'));
run;

Although I fail to see the need for that code, simply:

data outlib.&outname.;
  set inlib.&outname.;
run;

Does exactly the same job, with none of that fuss.

Contributor
Posts: 45

Re: Macro variable inside call execute

[ Edited ]

Thanks for the reply.

 

It is not exactly the same code, as in your case I have also a copy of FINAL_TABLE in the output library.

 

I could do this way nevertheless:

%macro SAVING(
	library, 
	input_table,
	output_table);

data &library..&output_table.;
set WORK.&input_table.;
run;

PROC DELETE DATA= &libray..&input_table.;
quit;
%mend;

But when I try this:

data _null_;
 call execute(cats('%saving(library=out_lib,input_table=final_table,output_table=',"&outname.".');'));
run;

I have this issue in front of the macrovariable outname:

ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

 Edit: This is how I create my macrovariable:

%let outname = p_201803;
Super User
Posts: 9,612

Re: Macro variable inside call execute

First of all, you can greatly reduce the complexity of the call execute with the macro variable:

call execute('%saving(temp,class,' !! "&outname" !! ');');

But then, you need to take care of timing when calling a macro that contains macro code with call execute:

libname temp '/tmp';

data work.class;
set sashelp.class;
run;

%macro SAVING(
	library, 
	input_table,
	output_table);

PROC COPY 
inlib=WORK outlib= &library.;
SELECT &input_table.;
run;

%put executing macro statements;
%if %sysfunc(exist(&library..&output_table.)) %then %do;
	PROC DELETE DATA= &library..&output_table.;
%end;

PROC DATASETS NOPRINT library=&library.;
    change &input_table.= &output_table.;
run;
quit;
%mend;

%let outname=class1;

data _null_;
call execute('%saving(temp,class,' !! "&outname" !! ');');
run;

data _null_;
call execute('%nrstr(%saving(temp,class,' !! "&outname" !! '));');
run;

Closely examine the logs from the two data _null_ / call execute steps. In the first, you will find that the %put happens BEFORE the first data step runs, while in the second, it happens where you intend your macro statements to execute. That is the function of the %nrstr() wrapped around the macro call.

Why does this happen?

When call execute pushes code onto the stack, and macro code is detected, the macro code is immediately handed over to the macro processor, while all data or procedure steps have to wait until the data step that makes the call execute has ended. %nrstr prevents immediate resolution of the macro statements contained inside, and so the macro keeps its timing.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,227

Re: Macro variable inside call execute

There was a typo, dot rather than comma.

data _null_;
 call execute(cats('%saving(library=out_lib,input_table=final_table,output_table=',"&outname.",');'));
run;

 

Super User
Posts: 10,626

Re: Macro variable inside call execute

Check system macro variable &syslast.

 

data class;
 set sashelp.class;
run;


data x;
 set &syslast ;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 107 views
  • 1 like
  • 4 in conversation