在 Proc SQL 中使用多个 end as 语句时出现语法错误

问题描述 投票:0回答:1

我使用下面的 proc SQL 代码来赋值并创建名称以 _val 结尾的变量。添加 _val 后,某些变量名称太长,因此我尝试使用 do 循环和 %if %then %else 有条件地缩短名称。

%macro rn_do;
proc sql noprint;
    create table validate_select2 as
    select *
    %do a=1 %to &checkbox_select_n;
        %do b=1 %to %eval(&&variable_checkbox_select_max&a);
            ,case when &b=1 and &&variable_checkbox_select&a.&&b. in (select valid_values from response_options_scan where variable="&&variable_checkbox_select&a") then "&&variable_checkbox_select&a&&b valid value"
            when &b=1 and &&variable_checkbox_select&a.&&b.="" and (select requirement_status from response_options_select where variable="&&variable_checkbox_select&a") in ("All Records (Backend data)","All Records", "Required") then "&&variable_checkbox_select&a&&b invalid value: missing"
            when &b=1 and &&variable_checkbox_select&a.&&b.="" and (select requirement_status from response_options_select where variable="&&variable_checkbox_select&a") then "&&variable_checkbox_select&a&&b invalid value: missing"
            when &b=1 and &&variable_checkbox_select&a.&&b.="" then "&&variable_checkbox_select&a&&b valid value: missing"
            when &b ne 1 and &&variable_checkbox_select&a.&&b.="" then "&&variable_checkbox_select&a&&b valid value: missing"
            when &b ne 1 and strip(&&variable_checkbox_select&a.&&b.) in (select valid_values from response_options_scan where variable="&&variable_checkbox_select&a") then "&&variable_checkbox_select&a&&b valid value"
            else "&&variable_checkbox_select&a&&b invalid value"
            %do c=1 %to 3;
                %if &&variable_checkbox_select&a=child&c._coadministered_vaccine %then %do;
                    end as child&c._coadministered_vax&b._val format=$100.
                %end;
                %else %if &&variable_checkbox_select&a=coadministered_vaccine_other %then %do;
                    end as coadministered_vax_other&b._val format=$100.
                %end;
                %else %do;
                    end as &&variable_checkbox_select&a.&&b._val format=$100.
                %end;
            %end;
        %end;
    %end;
    from validate_select;
quit;
%mend rn_do;

%rn_do;

我在日志中收到以下错误。 SAS 似乎对我的最终声明有异议,但我不知道为什么。

 NOTE: Line generated by the invoked macro "RN_DO".
 169               end as &&variable_checkbox_select&a.&&b._val format=$100.
                   ___
                   22
 NOTE 137-205: Line generated by the invoked macro "RN_DO".
 169               end as &&variable_checkbox_select&a.&&b._val format=$100.
                                                                ______
                                                                22
 ERROR 22-322: Syntax error, expecting one of the following: ',', AS, FROM.  
 
 ERROR 22-322: Syntax error, expecting one of the following: GROUP, HAVING, ORDER, WHERE.  
 
 NOTE: Line generated by the invoked macro "RN_DO".
 169               end as &&variable_checkbox_select&a.&&b._val format=$100.
                                                                ______
                                                                76
 ERROR 76-322: Syntax error, statement will be ignored.
sql sas syntax-error
1个回答
0
投票

我不太清楚你在做什么,但我认为问题是你有一个

%DO  c=1 to 3;
循环,它生成 3 个结束语句。所以第一个可能是有效的,但接下来的两个是错误的。

        %do c=1 %to 3;
            %if &&variable_checkbox_select&a=child&c._coadministered_vaccine %then %do;
                end as child&c._coadministered_vax&b._val format=$100.
            %end;
            %else %if &&variable_checkbox_select&a=coadministered_vaccine_other %then %do;
                end as coadministered_vax_other&b._val format=$100.
            %end;
            %else %do;
                end as &&variable_checkbox_select&a.&&b._val format=$100.
            %end;
        %end;

也许您只需要摆脱 %DO 循环即可。但这可能是错误的,因为我看不到大局。

© www.soinside.com 2019 - 2024. All rights reserved.