Home > Mobile >  Create columns with an array in a loop in sas
Create columns with an array in a loop in sas

Time:11-24

I want to create five target year columns to the work.komposit_prod throug a loop. I got following code:

proc sql noprint;
select distinct year into :targetyears1 - FROM work.QE_Target
ORDER by year;
quit;


proc sql noprint;
  select distinct Count(Jahr) into :Count_targetyears 
  FROM 
      (select distinct year FROM work.QE_Target);
quit;

%let max = &Count_targetyears;

data test ;
set work.komposit_prod;
Do i=1 to &max;
"ZZ_&&targetyears&i"n = .;

end;
run;

Somehow the variable "ZZ_&&targetyears&i"n reference couldn't be resolved. Can someone give me a hint? Thank you.

Kind regars, Ben

CodePudding user response:

Your do loop references a data step variable rather than a macro variable:

Do i=1 to &max;
    "ZZ_&&targetyears&i"n = .;
end;

You will need to convert this to a macro to run it:

%macro target_years;
    data test ;
        set work.komposit_prod;
        %do i=1 to &max;
            "ZZ_&&targetyears&i"n = .;
        %end;
    run;
%mend;
%target_years
  • Related