简体   繁体   中英

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.

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM