0
votes

I am new to SAS, so I apologize if this question is too simple. I have tried extensive searching about this and it's not clear to me how to proceed.

I am trying to initialize a new SAS data set with a feature corresponding to already-existing macro variables var1,...,varN. For simplicity assume that N is already defined as a macro variable in the generating process for these variables. I want to do something along the lines of the following code, which does not work:

%let var1 = 3;
%let var2 = 2.5;
%let var3 = 1;
[...]
%let varN = 2;


Data ABC;
  do i = 1 to &N;
    x = var&i.;
  end;
run;

I believe one of the problems is that unlike %do, the "i" is not a macro-variable. I have tried variants on cat(var,i), but it doesn't have the desired behavior since I want &var&i to resolve. Is there a way to make this work within a DATA step?

1

1 Answers

3
votes

I can't imagine a situation where doing this is actually a good idea, nevertheless, here's an answer. You can't use the standard macro variable &&var&i because then you'll need a %DO loop. To keep it in a data step use SYMGET.

%let var1=1;
%let var2=2;
%let var3=3;
%let N=3;

Data ABC;
    do i=1 to &N;
        x = symget(catt('var', i)); *character value;
        y = symgetN(catt('var', i)); *numeric value;
        output;
    end;
run;

proc print data=abc;
run;