SAS define a macro inside a data step - sas

I have some legacy SAS code that defines a macro inside a data step. How does this work? Does the macro get compiled for every iteration of DATA step?
data test;
set temp;
%macro cal_sum(iput);
%let a=&input.;
%mend;
REPORTING_DATE = &PROCDATE.;
call execute('%cal_sum(testval)');
run;

No. In this case, the macro is compiled before the data step executes. There are only three ways you can run / compile a macro for every iteration of a data step:
dosubl()
run_macro within an fcmp call
call execute - but note that any generated SAS code is executed AFTER the data step.
The way it works is:
data test; set temp; is sent to the stack, ready to be executed on the next step boundary
%macro cal_sum(iput);%mend; macro is compiled (not executed)
REPORTING_DATE = &PROCDATE.;run; is sent to the stack, and executed (as the run; statement is a step boundary)

The macro code, in this case a macro definition, gets fully resolved before the data step is compiled and run by the SAS executor.
So no, the macro is not compiled for every iteration of the DATA step.
Also, as you my be aware, a macro call coded inside a data step is not invoked for every iteration, however, whatever source code the macro call emitted will be.

Related

How is do loop working in this code ? Why call execute is working without loop ending?

Need to understand why call execute is working when it is outside the loop for each iteration ?Variable name will be overwritten every time so I should get result for last variable in my dataset rather that all 111 variables.
data _null_;
set basel.Data_Dictionary;
do i =1 to 111 ;
call symput('Varname',NAME);
%put &varname.;
end;
call execute ('%missimp(&varname.)');
run;
Not sure what you mean by outside the loop?
The first thing that program will do is print the value that the macro variable VARNAME has BEFORE the data _null_ step starts to the log. Note that macro code is processed first and then the resulting text is interpreted as the SAS code that you want to run. It would be less confusing to place the %put statement before the data statement.
Your datastep will "loop" over each observation in your source dataset. It will read an observation from your input data. The DO loop will cause it to set the macro variable VARNAME to the same value one hundred and eleven times. Then it will place a call to the macro named MISSIMP that will use the value of VARNAME (at the time the CALL EXECUTE statement ran). This will repeat until the SET statement tries to read past the end of input dataset. All of those macro calls will run after the current data step finishes.
A much simpler process would be just skip the CALL SYMPUT statement and use the value of NAME to generate the code to pass to CALL EXECUTE. Like this:
data _null_;
set basel.Data_Dictionary;
call execute(cats('%missimp(',NAME,')'));
run;

SAS Running out of memory while calling macros

When I need to call a macro several times, I've been using CALL EXECUTE in a DATA NULL step like so:
DATA _NULL_;
DO i=1 to 1000;
CALL EXECUTE ('%mymacro');
RUN;
This has worked fine for me up until now. However if I use this method to call %mymacro a million times (say), I get an "out of memory" error before it runs the macro once.
My naive understanding of this is that SAS attempts to "write out" the macro a million times before executing and thus runs out of memory during this process. Is this accurate? And: what are good ways to get around this?
You just need to understand how Call Execute works :
Basically ,Call Execute will parse the macro code immediately, but it queues up the resulting SAS steps until after the current data step finishes. In other words, you are potentially building up millions upon millions of lines of SAS code in memory that SAS is just storing up to be executed when that data _null_ step finishes. Eventually, this gets so large that SAS just crash.
Here's a couple of solutions :
1- Either add %nrstr() into your CALL EXECUTE statements.
2- Or change your data _null_ step to generate a file with the code and %include the file.
One option would be to chage the data step so that it actually creates a .sas file that contains the macro calls... and then %include it. For exmaple:
data _null_;
file "myfile.sas";
do i=1 to 1000;
put '%mymacro';
end;
run;
%include "myfile.sas";
This may fix the issue. Then again I'm not sure if SAS would like a .sas program that contains 1 million lines of code either. If the latter is the case, then simply break the program up into 10 .sas files each with 100k lines of code.

macro references execution in call execute

SAS documentation says the macro references in call execute are executed immediately. Does this code exemplify it?
%let var = abc;
data _null_;
call execute ('&var');
run;
Sort of. Here is a more complete example using value of the macro variable that is actual executable SAS code.
data _null_;
call symputx('var','data;run;');
run;
%put var= %superq(var);
data _null_;
call execute ('&var');
run;
You can see in the SAS log that the code that CALL EXECUTE() actually pushed onto the stack to run is the VALUE of the macro variable even though the single quotes would prevent the macro variable from expanding during the data _null_ step that is using the CALL EXECUTE() statement.
NOTE: CALL EXECUTE generated line.
1 + data;run;

SAS execute macro conditionally

I want to execute a macro conditionally, based on some loop variable in the dataset.
data alldat;
do i=1 to 5;
* here, a macro should be called ;
* that is accessing some array variable of the dataset ;
* e.g. %my_macro(my_array(i));
%put hello;
output;
end;
run;
proc print; run;
How is this possible?
If I execute this example code above, hello is only output once, while alldat contains 5 values, as one would expect. I want 5 hellos in my output.
Thank you!
If you want your data step loop to output hello 5 times then use a PUT statement instead of a %PUT statement.
In general macros are used to generate SAS code that is then executed. It really depends on what type of code your macro generates. If it only generates statements than can be used inside of a data step then call it once inside of your DO loop and the generated statements will run 5 times. So if your macro generates data step statements that could update the variable whose name is passed to it then your code might look like this.
data alldata;
array my_array var1-var5 ;
do i=1 to dim(my_array);
%my_macro(my_array(i));
put 'hello';
output;
end;
run;
Otherwise you could use CALL EXECUTE to generate the macro calls so that they can then run and generate their code after the data step stops. This data step will read values from an existing array and pass the values to the macro. So it will generate 5 calls to the macro for every observation in the input data. The generated macro calls will run after the data step stops.
data _null_;
set mydata;
array my_array var1-var5 ;
do i=1 to dim(my_array);
call execute(cats('%nrstr(%my_macro)(',my_array(i),');'));
call execute('%nrstr(%put hello;)');
end;
run;

Why does my macro behave differently with call execute()?

Using SAS, I often want to perform an action on each row of a dataset. To do so, I use a command I found on a tutorial : call execute(). As I'm not very familiar with SAS environment, I tend to use macro-functions to do anything I don't know how to and execute them with call execute().
However I have difficulties understanding how macro-language works exactly in SAS. I know that all macro references are resolved first, which provides a base-SAS code which is then executed (or am I already wrong ?). But I don't understand how it applies with call execute().
Let's consider the following code :
%macro prog1; /* %prog1 defines a macrovariable mv1 */
%global mv1;
data _null_;
call symputx("mv1","plop");
run;
%mend;
%macro prog2(var); /* prog2 puts it on the log */
%put PUT &var;
%mend;
%macro prog_glob; /* prog_glob executes prog 1 then prog2 */
%prog1;
%prog2(&mv1);
%mend;
I know there is no need for three macros here but this is a minimal version of my real code, which has this structure.
Now if I execute prog_glob :
%prog_glob;
I get PUT plop on the log, as expected.
But if I use it with call execute() (even if there is no need for loop here) :
data _null_;
mac=%NRSTR("%prog_glob");
call execute(mac);
run;
I get only PUT.
There is no error suggesting that the macrovariable is not defined so the %global statement worked. But somehow, prog2 was executed before prog1 base part (at least I think so) and mv1 is not defined yet.
My questions are :
Is my interpretation correct ?
Why does the result change when I use call execute ?
Depending on the precedent question answer, how should I fix it or is there a more convenient way to loop trough a column values ?
EDIT : My original code intends to rename the variables of several tables, which I have listed in a dataset. For each listed table, I want the following algorithm executed :
prog1 : store a list with all variables in a macrovariable (this is where I define mv equivalent)
prog2 : add a common suffix to these variables names
There is probably a more clever way to do this. Again, I'm not so familiar with SAS and I tend to over-use macros. If you want to show me a better way to do this, I'd be happy to chat but I don't expect you guys to rewrite all my code so an alternative to call execute would be enough for me to be grateful ! :)
Let's have a look at the documentation
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm
If an EXECUTE routine argument is a macro invocation or resolves to one, the macro executes immediately. However, any SAS statements produced by the EXECUTE routine do not execute until after the step boundary has been passed.
Note: Because macro references execute immediately and SAS statements do not execute until after a step boundary, you cannot use CALL EXECUTE to invoke a macro that contains references for macro variables that are created by CALL SYMPUT in that macro. See Interfaces with the Macro Facility, for an example.
This means, if you call it via call execute:
macro statements are executed immediately - those are:
1.1. first in %prog1: %global mv1; - so mv1 is defined but empty, no Apparent... warning
1.2. SAS statements from %prog1 are still deferred
Now %prog2 - here's only macro statement %PUT which puts (still empty) &mv1 variable. That what you see in the log
Now all the what gets executed immediately has been done. The data step which contains call execute ends.
SAS statements deferred from call execute are now being executed:
4.1. the dataset from prog1 sets the value for mv1.
And that's all :-)
EDIT: regarding your edit: try looking at this http://support.sas.com/kb/48/674.html
data _null_;
mac='%nrstr(%prog_glob)';
call execute(mac);
run;
or, more plainly, as you would see in the documentation...
data _null_;
call execute('%nrstr(%prog_glob)');
run;
or
%let prog=%nrstr(%prog_glob);
data _null_;
mac="&prog.";
call execute(mac);
run;
or, and I wouldn't really recommend this one, but you could also manually concatenate the macro quotes
data _null_;
mac=cats('01'x,'%prog_glob','02'x);
call execute(mac);
run;
The way you are running it, the macro statements get execute at run time and the data step is execute after the calling data step completes. You're not properly using %NRSTR for this context, as described by the documentation. You need to pass the macro, along with the quoting as text to the call routine.