useR
useR

Reputation: 3082

do loop within data step with unresolve variables

I have several parameters from proc sql select into: and I would like to store all of them to a dataset named parm. The following is the code I attempted:

I got the following parameter from previous step:

%let count = 4;
%let yymm1 = '1505';
%let yymm2 = '1504';
%let yymm3 = '1503';
%let yymm4 = '1502';

Here is the data step:

data parm;
    format yymm1 - yymm&count. $4.;
    array A(*) yymm1-yymm&count.;
    do i = 1 to &count.;
        A(i) = "&&yymm&i";
    end;
run;

Problem is that &i and &&yymm cannot be resolved.

Upvotes: 0

Views: 102

Answers (2)

Tom
Tom

Reputation: 51611

The problem with your code is that you are referencing the undefined macro variable I. Notice that your DO loop is creating the data set variable I. Either recode to use a macro %DO loop or modify the program to use the value of the data set variable I to build the name of the macro variable you want to retrieve.

do i=1 to dim(a); 
   a(i) = symget(cats('yymm',i));
end;

Upvotes: 0

Haikuo Bian
Haikuo Bian

Reputation: 906

While I echo @Joe 's comment, here are my suggestions on your given situation. First make sure if the macro variable values contain single quotes or NOT. You have it in your macro variable definition, while length=4 in your data step won't cut it for you. Then like @Joe suggested, you need to separate your macro variable and data step variable by doing something like the following:

%let count = 4;
%let yymm1 = '1505';
%let yymm2 = '1504';
%let yymm3 = '1503';
%let yymm4 = '1502';

data parm;
    format yymm1 - yymm&count. $6.;
    array A(*) yymm1-yymm&count.;

    do i = 1 to &count.;
        A(i) = symget(cats('yymm',i));
    end;
run;

Upvotes: 2

Related Questions