Testing for an empty parameter in a SAS Macro

后端 未结 2 1859
半阙折子戏
半阙折子戏 2021-01-14 12:46

For example, i have a macro program

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


        
2条回答
  •  旧巷少年郎
    2021-01-14 13:08

    For this answer I will cite Chang Chung's seminal paper, "Is This Macro Parameter Blank", as it is excellent and goes into great detail about your options here.

    The "best" option follows (ie, using the recommended method from the paper above). Note the initial %test macro doesn't return any rows for the blank parameter, while the second %test2 does.

    There are simpler ways to test for a macro parameter being blank that work in most cases, and read the paper for more details about the simpler options.

    %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=);
    

提交回复
热议问题