A macro variable is not initialized after the% let operator in sas

I want to create something in SAS that works as an Excel search function. Basically, I set the values โ€‹โ€‹for the macro variables var1 , var2 , ... and I want to find their index number according to the ref table. But at the data stage, I get the following messages.

 NOTE: Variable A is uninitialized. NOTE: Variable B is uninitialized. NOTE: Variable NULL is uninitialized. 

When I print the variables &num1 , &num2 , I get nothing. Here is my code.

 data ref; input index varname $; datalines; 0 NULL 1 A 2 B 3 C ; run; %let var1=A; %let var2=B; %let var3=NULL; data temp; set ref; if varname=&var1 then call symput('num1',trim(left(index))); if varname=&var2 then call symput('num2',trim(left(index))); if varname=&var3 then call symput('num3',trim(left(index))); run; %put &num1; %put &num2; %put &num3; 

I can get the correct values โ€‹โ€‹for &num1 , &num2 , .. if I enter varname='A' in the if-then . And if I subsequently change the statement to varname=&var1 , I can still get the required output. But why is this so? I donโ€™t want to enter the actual string value and then change it back to a macro variable to get the result every time.

+5
source share
1 answer

Solution to an immediate problem

You need to wrap the macros in double quotes if you want the SAS to treat them as string constants. Otherwise, it will process them just like any other random bits of text that it finds in your data step.

Alternatively, you can override macros to include quotes.

As an additional feature, you can use the symget or resolve functions, but they are usually not needed if you do not want to create macro variables and use them again at the same data step. If you use them as a substitute for double quotes, they tend to use a lot more CPUs, since by default they evaluate macro vars once per line - usually macro vars are evaluated only once at compile time before your code done.

The best approach?

For what you are doing, you donโ€™t need to use a dataset at all - you can instead define a custom format that gives you much more flexibility in how you can use it. For instance. this creates a lookup format:

 proc format; value lookup 1 = 'A' 2 = 'B' 3 = 'C' other = '#N/A' /*Since this is what vlookup would do :) */ ; run; 

Then you can use this format:

 %let testvar = 1; %let testvar_lookup = %sysfunc(putn(&testvar, lookup.)); 

Or in the data step:

 data _null_; var1 = 1; format var1 lookup.; put var1=; run; 
+8
source

All Articles