1
votes

I need to define a variable called test_var based on what the input is to a macro. Here is a simplified version of what I'm trying to achieve in SAS:

%macro testing(blah);

    %if &blah. = 1 %then %do;
    data _null_;
        call symput('test_var',"a1");
    run;
    %end;
%mend;

%testing(1);
%put &test_var.;

But i get the error: WARNING: Apparent symbolic reference TEST_VAR not resolved.

2

2 Answers

2
votes

It is a simple problem of macro variable scoping. You are defining the macro variable inside of the macro TESTING and trying to reference it after the macro has stopped running.

If the macro variable you are trying to set does not already exist then it will be defined in the local scope. So either define the macro variable before calling the macro or have the macro create a GLOBAL macro variable instead of a local one.

You can use the %SYMEXIST() macro function to check whether you need to define the macro as global or not and then use the %GLOBAL statement to define it in the GLOBAL scope.

%if not %symexist(test_var) %then %global test_var;

Also if you switch to using the newer CALL SYMPUTX() function instead of the old style CALL SYMPUT() function you can use the optional third argument to tell it to force the macro variable in the GLOBAL symbol table instead of following the normal scoping rules.

call symputX('test_var',"a1",'g');

PS: You also have not told the macro where the macro variable BLAH is coming from. It is not a parameter and it is not defined in any %LOCAL or %GLOBAL statement. Personally I consider the use of this type of "magic" macro variables in macro code a poor coding style that can cause maintenance issues. At a minimum add a comment about where it is coming from. Or use the %SYMEXIST() function to make sure it exists before referencing it.

1
votes

You can resolve this in one of two ways:

  1. Define &test_var before you run your macro, e.g. %let test_var=;.
  2. Declare it as a global macro variable within the macro, e.g. %global test_var;