Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Macro variable getting set to the name of the macro

Reply
Frequent Contributor
Posts: 82

Macro variable getting set to the name of the macro

   When I run this progrma:

data _null_;
   set info1;
   if compress(upcase(a),'','cops') eq 'abc' then call symputx('macro_var1',b,'g'); /*if condition evaluates to false*/
 
  run;

    %put Macro var1 is: &macro_var1;
   


I get Output:
Macro var1 is: &macro_var1

I was expecting output:
Macro var1 is:

i.e. the macro variable should be set to a null (blank) value. Why did the macro variable value get set to the name of the macro?
The dataset info1 looks like (2 by 2 matrix):

Name   user12
Salary 100

Super User
Posts: 11,343

Re: Macro variable getting set to the name of the macro

Posted in reply to eagles_dare13

I can't duplicate this. I suspect that at some point in testing this you accidentally set the value.

Try adding

%let macro_var1=;

before your data step and see if that still happens;

Super User
Super User
Posts: 7,074

Re: Macro variable getting set to the name of the macro

Posted in reply to eagles_dare13

Since the IF condition is false the CALL SYMPUT does not run so the macro variables value is not changed.

You are just seeing the value that it had before the data step ran.

SAS Employee
Posts: 3

Re: Macro variable getting set to the name of the macro

Posted in reply to eagles_dare13

Both responders above have hit the nail on the head.  As your if condition is false, the call symput code is never run and thus your code never initializes the new macro variable macro_var1.   Without initializing, it is an unknown macro variable when the put statement is run.  When this occurs, the put statement simply outputs the unknown macro variable name.

This can be quickly solved by first initializing the macro variable:

%let macro_var1=;

data _null_;

   set info1;

   if compress(upcase(a),'','cops') eq 'abc' then call symputx('macro_var1',b,'g'); /*if condition evaluates to false*/ 

run;

%put Macro var1 is: &macro_var1;

Or by setting the variable with another symput statement when the condition is true.

data _null_;

   set info1;

   if compress(upcase(a),'','cops') eq 'abc' then call symputx('macro_var1',b,'g'); /*if condition evaluates to false*/ 

   else call symputx('macro_var1','new value','g'); /*if condition evaluates to true*/

run;

%put Macro var1 is: &macro_var1;

In either case, the macro variable will always be a known variable.

Ask a Question
Discussion stats
  • 3 replies
  • 306 views
  • 2 likes
  • 4 in conversation