3
votes

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

3

3 Answers

1
votes

SAS helpfully stores them in a table for you already, you just need to parse out the ones you want. The table is called SASHELP.VMACRO or DICTIONARY.MACROS

Here's an example:

%let var=1;
%let var2=3;
%let var4=5;

proc sql;
create table want as
select * from sashelp.vmacro
where name like 'VAR%';
quit;

proc print data=want;
run;
1
votes

I think the real issue is the inconsistent behavior of the stored process. It only creates the 0 and 1 variable when there are multiple selections. I think that your example is a little off. If the value of VAR_0 is three then their should be a VAR_3 macro variable. Also the value of VAR_ and VAR_1 should be set to the same thing.

To fix this in the past I have done something like this. First let's assign the parameter name a macro variable so that the code is reusable for other programs.

%let name=VAR_;

Then first make sure the minimal macro variables exist.

%global &name &name.0 &name.1 ;

Then make sure that you have a count by setting the 0 variable to 1 when it is empty.

%let &name.0 = %scan(&&&name.0 1,1);

Then make sure that you have a 1 variable. Since it should have the same value as the macro variable without a suffix just re-assign it.

%let &name.1 = &&&name ;

Now your data step is easier.

data want ;
  length var $32 value $200 ;
  do i=1 to &&&name.0 ;
    var=cats(symget('name'),i);
    value=symget(var);
    output;
  end;
run;
0
votes

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;