Select SAS选择进入动态命名

Select SAS选择进入动态命名,select,sas,proc,select-into,Select,Sas,Proc,Select Into,我正在尝试将SAS proc sql select into用于8种不同的场景 %MACRO Loop(); proc sql; %do i=1 %to 8; select total_value into :Sol_&i. separated by ',' from Master.adjusted_hours where Solicitation = "&i.&qu

我正在尝试将SAS proc sql select into用于8种不同的场景

  %MACRO Loop();
     proc sql;
         %do i=1 %to 8;
         select total_value
         into :Sol_&i. separated by ','
         from Master.adjusted_hours
         where Solicitation = "&i.";
    %end;
quit;
%mend;

%Loop();
但是,当我使用%put函数时,宏变量无法识别。错误为“明显符号参考SOL_1未解决”


如何将该值存储在此宏变量中,并在以后的代码中调用它?

您需要将SOL_1 SOL_2等声明为全局宏变量。我不确定你的数据中有什么,所以我创建了一些虚拟数据

%global SOL_1 SOL_2 SOL_3 SOL_4 SOL_5 SOL_6 SOL_7 SOL_8;

data adjusted_hours;
    do x = 1 to 8;
        solicitation=put(x, 1.);
        do total_value = 1 to 10;
            output;
        end;
    end;
    drop x;
run;

%MACRO Loop();
 
     proc sql noprint;
     %do i = 1 %to 8;
         select total_value
         into : Sol_&i. separated by ','
         from adjusted_hours
         where Solicitation = "&i.";
     %end;
    quit;

%mend;

%Loop();

%put _USER_;
如果您想要完全避免使用宏,并且必须声明%全局变量,那么只要您的数据集是按请求排序的,这里就有一个数据步骤解决方案

data _null_;
    set adjusted_hours;
    by solicitation;
    format temp $50.;
    retain temp ;
    temp=CATX(',',temp, total_value);
    
    if last.solicitation then do;
        call symputx(CATS('SOL_', solicitation), temp);
        call missing(temp);
    end;
run;
Partial log:
 
 GLOBAL SOL_1 1,2,3,4,5,6,7,8,9,10
 GLOBAL SOL_2 1,2,3,4,5,6,7,8,9,10
 GLOBAL SOL_3 1,2,3,4,5,6,7,8,9,10
 GLOBAL SOL_4 1,2,3,4,5,6,7,8,9,10
 GLOBAL SOL_5 1,2,3,4,5,6,7,8,9,10
 GLOBAL SOL_6 1,2,3,4,5,6,7,8,9,10
 GLOBAL SOL_7 1,2,3,4,5,6,7,8,9,10
 GLOBAL SOL_8 1,2,3,4,5,6,7,8,9,10
data _null_;
    set adjusted_hours;
    by solicitation;
    format temp $50.;
    retain temp ;
    temp=CATX(',',temp, total_value);
    
    if last.solicitation then do;
        call symputx(CATS('SOL_', solicitation), temp);
        call missing(temp);
    end;
run;