sassas-macroenterprise-guidesas-stored-process

Get values of Macro Variables in a SAS Table


I have a set of input macro variables in SAS. They are dynamic and generated based on the user selection in a sas stored process.

For example:There are 10 input values 1 to 10.
The name of the macro variable is VAR_. If a user selects 2,5,7 then 4 macro variables are created. 
&VAR_0=3;
&VAR_=2;
&VAR_1=5;
&VAR_2=7;

The first one with suffix 0 provides the count. The next 3 provides the values.

Note:If a user select only one value then only one macro variable is created. For example If a user selects 9 then &var_=9; will be created. There will not be any count macro variable. I am trying to create a sas table using these variables.

It should be like this

OBS    VAR
-----------
1      2
2      5
3      7
-----------

This is what I tried. Not sure if this is the right way to do approach it. It doesn't give me a final solution but I can atleast get the name of the macro variables in a table. How can I get their values ?

data tbl1;
do I=1 to &var_0;
VAR=CAT('&VAR_',I-1);
OUTPUT;
END;
RUN;
PROC SQL;
CREATE TABLE TBL2 AS 
SELECT I,
CASE WHEN VAR= '&VAR_0' THEN '&VAR_' ELSE VAR END AS VAR
from TBL1;
QUIT;

Thank You for your help.

Jay


Solution

  • I don't understand your numbering scheme and recommend changing it, if you can; the &var_ variable is very confusing.

    Anyway, the easiest way to do this is SYMGET. That returns a value from the macro symbol table which you can specify at runtime.

    %let VAR_0=3;
    %let VAR_=2;
    %let VAR_1=5;
    %let VAR_2=7;
    
    data want;  
      do obs = 1 to &var_0.;
        var = input(symget(cats('VAR_',ifc(obs=1,'',put(obs-1,2.)))),2.);
        output;
      end;
    run;