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

Syntax errors when using multiple end as statements in Proc SQL

提问人:mvanduym 提问时间:10/12/2023 最后编辑:mvanduym 更新时间:10/12/2023 访问量:59

问:

我正在使用下面的 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 语法错误

评论

0赞 Reeza 10/12/2023
数组在这里是一个选项,而不是 SQL?
0赞 Tom 10/12/2023
%DO C 循环有什么用途?它不能按书面形式工作,因为它为单个 CASE 关键字生成三个 END 关键字。如果要生成三个不同的变量,则 %DO C 语句需要位于 CASE 子句开始之前。
0赞 Tom 10/12/2023
显示要宏生成的 SAS 代码。
0赞 Tom 10/12/2023
您没有解释所引用的宏变量的含义,也没有解释它们具有哪些类型的值。非常奇怪的是,宏使用既不是输入也不是局部的宏变量,甚至没有任何注释来解释它们的预期来源。

答:

1赞 Quentin 10/12/2023 #1

我不太清楚你在做什么,但我认为问题是你有一个循环,它正在生成 3 个结束语句。因此,第一个可能是有效的,但接下来的两个是错误的。%DO c=1 to 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循环。但这可能是错误的,因为我看不到大局。

如果您在打开选项 MPRINT 的情况下运行代码(我总是打开它),它将显示生成的代码,应该可以帮助您查看是否正在生成 3 个语句,正如我怀疑的那样。end

评论

0赞 mvanduym 10/12/2023
我在 MPRINT 上运行代码,它确实生成了 3 个结束语句。取出 do 循环确实解决了问题,但仍然想知道我是否可以在不添加额外的 %else %if 语句的情况下完成同样的事情。
0赞 Quentin 10/12/2023
抱歉,我无法充分说明每个宏变量的值,无法建议如何摆脱 %IF/%ELSE 块以生成 END 语句。你最好发布一个新问题,在其中给出一个小例子,说明数据、你的目的和你当前的方法(也许没有宏,只是一个 SQL 步骤)。可能有更简单的 DATA 步骤方法。