BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JChambo
Fluorite | Level 6

I did some test code and SELECT INTO : managed to get a macro variable into a nested macro. I tried to get the code setup as similarly to yours as I could. Can you run the below and see if the macro variable assignment works for you?

 

When I run it both "NOTE: !!! ... !!!" lines in the log report the same number for outer_macro_variable and inner_macro_variable.

 

data tem;
sourcevar=round(rand('UNIF',1,1000));
run;

%macro outer;

%global outer_macro_var;

proc sql;
select sourcevar
into : outer_macro_var
from tem;

%put NOTE: !!! outer_macro_var=&outer_macro_var !!!;

%macro inner;
%local inner_macro_var;
%let inner_macro_var= &outer_macro_var;

data _null_;
put "NOTE: !!! inner_macro_var=&inner_macro_var !!!";

%mend;

%inner;

%mend;

data _null_;
call execute('%outer');
DonnieJ
Obsidian | Level 7

I have narrowed it down to my CALL EXECUTE causing the problem. If I call the macro normally my SELECT INTO will assign the variable. When my macro is called via CALL EXECUTE it does not assign the variable. I have a feeling this is an issue with my environment. I have notified SAS technical support. 

DonnieJ
Obsidian | Level 7

Found this.... Now my code is working

 

Usage Note 23134: Macro variables created with a CALL SYMPUT or SYMPUTX statement or an INTO clause do not resolve when invoked by the CALL EXECUTE routine

 

 

If a CALL EXECUTE routine argument is invoked via a macro invocation or resolves to one, the macro code executes immediately. However, any SAS® statements (such as CALL SYMPUTX and INTO) that are produced by a CALL EXECUTE routine do not execute until after a step boundary has been passed.

Because of this, the %NRSTR function has to be used in the CALL EXECUTE syntax. This enables you to reference the variables created by CALL SYMPUTX or INTO in the code generated by CALL EXECUTE. This workaround delays execution of the macro within the DATA step that contains the CALL EXECUTE.

The use of %NRSTR in the following example masks the call to the %TEST macro when the argument to the CALL EXECUTE routine is evaluated. This causes the %TEST macro invocation to be pushed onto the input stack. Prior to using %NRSTR, all the non-macro statements generated were pushed to the input stack. %NRSTR delays the execution of the %TEST macro until after the RUN statement for the DATA step containing the CALL EXECUTE statement.

Here is an example:

 

 /*sample data set*/
   data a;
     value='Hello';
   run;

   /*sample macro*/
   %macro test(val);
 
   data _null_;
     call symput('num',200);
   run;

   %if &num eq 200 %then %do;
     proc print data=&val;
     run;
   %end;

   %mend;

   /*this will fail*/
   data _null_;
     temp='a';
     call execute('%test('||temp||')');
   run;

   /*work-around*/
   data _null_;
     temp='a';
     call execute('%nrstr(%test('||temp||'))');
   run;

 

Starting with SAS® 9.3 M2, the new DOSUBL function can be used instead of CALL EXECUTE. Here is an example:

Smiley Sad

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
  • 17 replies
  • 4467 views
  • 1 like
  • 5 in conversation