Testing an empty parameter in a SAS macro

For example, I have a macro program

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

      

Basically, I made a selection of observations using parameter 3 (Male or Female). Now I want to create a third option: keep both observations in Male and Female when parameter3 is empty (without declaring the value of this parameter).

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

      

Can you tell me how can I do this please?

+3


source to share


2 answers


For this answer, I'll quote Chang Chang's original article, "Is This Macro Parameter Blank" , as it is excellent and goes into great detail about your options here.

What follows is the "best" option (ie using the recommended method from the above document). Note that the original macro %test

does not return strings for an empty parameter, while the second %test2

does.



There are simpler ways to check that a macro parameter is empty, which works in most cases, and read the document for more details on 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=);

      

+4


source


Agree with Joe. One of the nice things they do in this article is testing for null, and their test will return whether the parameter is null or has spaces in it. It is sometimes useful to have a test that distinguishes between a parameter that is null and one that has spaces. Mentioned in the document %length(%superq( ))

as a possible test for null. For example below, this allows you to specify an empty value for the floor and get a different result than you when the floor is null.



data class;
  set sashelp.class;
  if _n_=1 then sex='';
run;

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

%test2(sex=)
%test2(sex=%str( ))

      

+4


source







All Articles