BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

suppose to have the following piece of code: 

 

%macro freq_for_value(data=, variable=, filter_value=);

    %if &filter_value. ne %str() %then %do;
        proc freq data=&data. (where=(&variable. = "&filter_value."));
            tables &variable.;
            title "Frequency for &variable. with value &filter_value.";
        run;
    %end;
    %else %do;
        proc freq data=&data.;
            tables &variable.;
            title "Frequency for all values of &variable.";
        run;
    %end;

%mend;

This macro basically performs proc freq in two different conditions: 1) if a variable takes a specific value and 2) if not. 

I would like to run this macro but I have many lines of code after the first proc freq that I have to repeat (and that are identical) after the second proc freq like for example the following: 

 

%macro freq_for_value(data=, variable=, filter_value=);

    %if &filter_value. ne %str() %then %do;
        proc freq data=&data. (where=(&variable. = "&filter_value."));
            tables &variable.;
            title "Frequency for &variable. with value &filter_value.";
        run;
        /*I add 10 rows of output formatting*/   
      ............................................................................................
      .............................................................................................
      ..............................................................................................

    %end;
    %else %do;
        /* If no filter value, run PROC FREQ on the entire data set */
        proc freq data=&data.;
            tables &variable.;
            title "Frequency for all values of &variable.";
        run;
    %end;
 /*I add 10 rows of output formatting*/   
      ............................................................................................
      .............................................................................................
      ..............................................................................................

%mend;

Is there a way or a condition that could be declared at the beginning in order to prevent to repeat the coding after proc freq (output formatting) that would make the macro unnecessarily long and redundant? In other words I'm referring to something that, based on the fact that the variable can or not take a specified value, performs proc freq in a way or in the other one. 

 

Thank you in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I am not sure I understand what the question is.  If you want to run the same code afterwards just place it beyond the second %END.  That way you do not have two copies.

 

But you could simplify the current code by using the macro logic to set macro variables that you use to generate the code.

%macro freq_for_value(data=, variable=, filter_value=);
  %local where ;
  %if %length(&filter_value.)  %then %do;
     %let where=where=(&variable. = "&filter_value.");
title "Frequency for &variable. with value &filter_value.";
  %end;
  %else %do;
title "Frequency for all values of &variable.";
  %end;

proc freq data=&data. (&where);
  tables &variable.;
run;

/* Other code goes here */

%mend;

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

I am not sure I understand what the question is.  If you want to run the same code afterwards just place it beyond the second %END.  That way you do not have two copies.

 

But you could simplify the current code by using the macro logic to set macro variables that you use to generate the code.

%macro freq_for_value(data=, variable=, filter_value=);
  %local where ;
  %if %length(&filter_value.)  %then %do;
     %let where=where=(&variable. = "&filter_value.");
title "Frequency for &variable. with value &filter_value.";
  %end;
  %else %do;
title "Frequency for all values of &variable.";
  %end;

proc freq data=&data. (&where);
  tables &variable.;
run;

/* Other code goes here */

%mend;

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 336 views
  • 1 like
  • 2 in conversation