SAS用数组循环

时间:2014-11-27 10:06:53

标签: arrays sas

我首先存储大小为3的数组的字符元素。

data _null_;

    array fruit[3] $10 ("apple" "orange" "lemon");

    call symput("fruit1", fruit1);
    call symput("fruit2", fruit2);
    call symput("fruit3", fruit3);

run;

我想打印数组fruit的所有元素,

%put &fruit1;
%put &fruit2;
%put &fruit3;

是否可以使用do-loop?

2 个答案:

答案 0 :(得分:1)

我更喜欢SQL方法来创建变量。这里有两种显示宏变量的方法,一种是使用数据步骤,另一种是使用宏do循环:

*Generate sample data;
data have;
    input fruit $20.;
    cards;
    apple
    banana
    grapes
    ;
run;

*Create macro variables;
proc sql noprint;
    select fruit 
    into :fruit1-:fruit1000
    from have;
quit;

*Store number of macro variables;
%let nobs=&sqlobs.;

%put &nobs;

*Display macro variables in data step;
data _null_;
    do i=1 to &nobs.;
        have=symget(catt("fruit", i));
        put have;
    end;
run;

*Display macro variables using macro logic;
%macro display_vars;
    %do i=1 %to &nobs.;
        %put &&fruit&i.;
    %end;
%mend;
%display_vars;

答案 1 :(得分:0)

data _null_;

  array fruit[3] $10 ("apple" "orange" "lemon");
  length all_fruit $100;

  do i =1 to dim(fruit);
    all_fruit = compress(all_fruit||'|'||fruit[i]);
    put fruit[i];
  end;

  call symputx("allfruit",all_fruit);

 run;

%put &allfruit