BookmarkSubscribeRSS Feed
kk89
Calcite | Level 5

How do two or more if statements work within a By statement. I was looking at the macro suppress in this link: https://blogs.sas.com/content/sgf/2016/04/13/automatic-data-suppression-in-sas-reports/

(and also below) and I found that once the code in blue ran, the macro ran the code in green. It almost seemed like a loop.

 

 

%macro suppress(dsname=,supclass=);
 
   proc sort data=&dsname;
      by &supclass count;
   run;
 
   %let sup_flag = 0;
   data &dsname (drop=_supnum_);
      set &dsname;
      by &supclass count;
      if first.&supclass then _supnum_ = 0;
      if (1 le count le &sup_max) or (_supnum_ eq 1 and count ne .S) then
      do;
         count = .S;
         call symput('sup_flag','1');
      end;
      if (count eq .S) then _supnum_ + 1;
   run;
 
%mend suppress;

 

 

 

1 REPLY 1
Tom
Super User Tom
Super User

The IF statements are not "within" BY statements.

 

The first IF statement is taking advantage of the fact that when using a BY statement SAS will create flag variables that will indicate if the current observation is the first or last in the current BY group.  So it resets the counter variable the data step is creating to zero when a new group starts.

 

Your guess on the second IF statement is going to be better than mine.  Especially since it is referencing a macro variable, SUP_MAX,  that you have not defined anywhere.  (Programming hint: Do not use "magic" macro variables in macro code.  That is a macro variable that is suddenly used by the macro but has never been defined by the macro and is not an input parameter of the macro.  At the very least add a comment at the top of the macro stating that the macro will be using this externally defined macro variable.)

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Save $200 when you sign up by March 14!

Register 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
  • 384 views
  • 0 likes
  • 2 in conversation