BookmarkSubscribeRSS Feed
Lakshmi_G90
Calcite | Level 5

Hi All,

Hope you're doing well !!

I created global macro variables from 2 columns in a table i.e, Global_macro. Column_A values will be each global macro variable name and the corresponding each value of Column_B are stored as the macro variable value respectively.

Column_A  Column_B

Var       put(today(),date7.)

Sum       245866

Max       max(135,6546,13)

 

data _null_;
do i=1 to max
set work.Global_macro nobs=max;
call symput(Column_A,Column_B);
end;
run;

Some of the macro variables are holding the values as "put(today(),date7.)". When we call the macro variables in SAS program, these're resolving as it is, but not executing the Put function and resolving as the final value in numeric - 12052020. 

%put value of &VAR;
resolving as put(today(),date7.) .

When we try the below code then working fine. 
data _null_;
var=&VAR;
call symput('VAR',var);
run;

%put Value of &VAR;
resolving to 12052020.

I have been trying this code dynamically to apply for all macro variables from the table Global_macro as below.

data _null_;
set work.Global_macro;
Col=trim("&")||Column_A;
call symput(Column_A,Col);
run;

ERROR: The text expression &Var contains a recursive reference to the macro
variable VAR. The macro variable will be assigned the null value.


Could someone please suggest me the solution on this.

Many Thanks in Advance !!

Best Regards,

6 REPLIES 6
ballardw
Super User

Better might be to explain what you are actually doing. Taking values from a data set, creating (obtuse) macro variables and then using in the form you show really appears to be a very complicated way do something. Or an exercise in intention obfuscation.

 

I am going to guess that perhaps one element of what you want might be the symget function. Maybe. I can't really tell what you are actually doing. The Symget function will return the value of a macro variable such as

 

value = symget('var') ;

to pull the actual value of a macro variable named Var. Note that you do not use & to reference the name of the variable.

Tom
Super User Tom
Super User

Also what is the plan to use this metadata?  Does it work now?  If so then why are you trying to change it?

 

It looks like COLUMN_B contains the snippet of CODE that you want to use and COLUMN_A has the name of a variable. 

Your first data step is storing the value B into the macro variable named in A.  What did you plan to do with these macro variables?

 

But the second one is trying to do something different. Why? What?

 

Did you want to use that metadata to generate actual SAS code instead of just creating macro variables?

filename code temp;
data _null_;
  set global_macro ;
  file code ;
  put column_a '=' column_b ';' ;
run;

data want;
  set have;
  %include code ;
run;

Or did you want to somehow use the a data step to evaluate the code and put the resulting value into a macro variable with that name?

filename code temp;
data _null_;
  set global_macro ;
  file code ;
  put 'call symputx(' column_a :$quote. ','  column_b ');' ;
run;

data _null_;
  %include code ;
run;

 

 

Lakshmi_G90
Calcite | Level 5

The actual plan of this is we need to create multiple global macro variables from Column_A and call them in further SAS programs.
We have created macro variables from Column_A values.
But after calling, some of them are resolving as the snippet of code like- Put(today(),date7.).
We are expecting them to resolve as 13052020. For this we need to write further step as below.

data _null_;
var=&VAR;
call symput('VAR',var);
run;

Then the VAR is working fine, and it's resolving to "13052020" as expected.
Indeed we need the same code should run for multiple macro variables in dynamic way.
That's why I've been trying the below code after creating global macro variables from Column_A.

data _null_;
do i= 1 to max;
set work.Global_macro nobs=max;
Col=trim("&")||Column_A;
call symput(Column_A,Col);
end;
run;

In the above step already macro variables are existing, so if run the code, this step-(Col=trim("&")||Column_A;) will call each global macro variable and will assign the value to Col variable, in this step onlty the code snippet values will execute the function and give the numeric results as 13052020..
In further step- (call symput(Column_A,Col);) again same multple global macro variables will be re-assigned with the numeric results of Col,
this code will execute in multiple iterations until the last value in Column_A.

But here we are getting the error:"The text expression &VAR contains a recursive reference to the macro variable VAR" for all other macro variables also.

Seems the existing macro variables are not recognized by the SAS at the first step - Col=trim("&")||Column_A;

 

Please suggest me the solution for this.

 

Thanks & Regards,

Tom
Super User Tom
Super User

You need to use the value of column_b as CODE, not as text to be passed to macro variables.

I posted before how to do it.  Use a datastep to generate the code from the metadata file. Then execute the code. So either write the generated code to a file like in my example

filename code temp;
data _null_;
  set global_macro ;
  file code ;
  put 'call symputx(' column_a :$quote. ','  column_b ');' ;
run;

data _null_;
  %include code ;
run;

or use CALL EXECUTE() to push the generated code onto the stack to run after the data step.

 

Or you could change the contents of COLUMN_B to be MACRO CODE and not SAS CODE.

Column_A       Column_B
date_string    %sysfunc(today(),date9.)
date_value     %sysfunc(today())
date_literal   "%sysfunc(today(),date9.)"d
sum            245866
max            %sysfunc(max(135,6546,13))
string         Company A
string_literal 'Company A'

 Then when you use the macro variable the macro functions will execute and the value the macro processor will return to SAS to use as part of the code you want to run will be the results the macro functions.

Kurt_Bremser
Super User

What is it that makes people intentionally complicate code?

This

data _null_; 
do i=1 to max
set work.Global_macro nobs=max;
call symput(Column_A,Column_B);
end;
run;

is equivalent to the much simpler

data _null_;
set work.Global_macro;
call symput(Column_A,Column_B);
run;

So why are you needlessly complicating things?

 

And I guess the same kind of confused thinking leads you to the disastrous abuse of the macro processor.

 

What is it that you want to solve with all this code obfuscation? Not the macro-diddling, the BUSINESS CASE.

Lakshmi_G90
Calcite | Level 5

🙂

@Kurt_Bremser , thanks for your suggestion, yes , will definitely follow your instructs.

 

Any luck for me on my issue of the resolving macro variables which are holding the values with SAS functions in dynamical way. Please suggest me.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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