测试SAS宏中的空参数

时间:2015-07-28 20:15:44

标签: sas sas-macro

例如,我有一个宏程序

%macro test(parameter1= , parameter2=, parameter3=);
  DATA data_gender;
  SET data_input(WHERE=(gender=parameter3));
  RUN;
  .....
%mend;

基本上,我使用参数3(男性或女性)进行了一系列观察。现在我想创建第三个选项:当参数3为空时(在不声明此参数的值的情况下)保持Male和Female中的两个观察值。

%test(parameter1=xxx , parameter2=yyy, parameter3=);

你能告诉我该怎么办?

2 个答案:

答案 0 :(得分:4)

对于这个答案,我会引用Chang Chung的开创性论文"Is This Macro Parameter Blank",因为它非常出色,并且在这里详细介绍了你的选择。

" best"选项如下(即使用上述论文中推荐的方法)。请注意,初始%test宏不会返回空白参数的任何行,而第二个%test2则会返回。

在大多数情况下,有一些更简单的方法可以测试宏参数是否为空,并阅读论文以获取有关更简单选项的更多详细信息。

%macro test(parameter1= , parameter2=, sex=);
  DATA data_gender;
  SET sashelp.class(where=(sex="&sex."));
  RUN;
%mend;

%test(sex=M);
%test(sex=F);
%test(sex=);

%macro test2(parameter1= , parameter2=, sex=);
  DATA data_gender;
  SET sashelp.class(
    %if %sysevalf(%superq(sex) ne,boolean) %then %do;
        where=(sex="&sex.")
        %end;
    );
  RUN;
%mend;

%test2(sex=M);
%test2(sex=F);
%test2(sex=);

答案 1 :(得分:4)

Agree with Joe's answer. One good point they make in the paper is they are testing for blank, and their test will return true whether a parameter is null or has blanks in it. Sometimes it is useful have a test that differentiates between a parameter that is null and one that has blanks. The paper mentions group by as a possible test for null. For example below, this allows you to specify a blank value for sex, and get a different result than you do when sex is null.

%length(%superq( ))