SUPPORT / SAMPLES & SAS NOTES
 

Support

Sample 37763: Conditionally print a LINE statement in PROC REPORT

DetailsOutputAboutRate It

A LINE statement value can be conditionally printed by using a text string and adjusting the format length.

A LINE statement from a COMPUTE BEFORE and a COMPUTE AFTER block will execute every time the COMPUTE block is processed. When a LINE statement is within an IF statement, the LINE statement will still be executed even when the IF condition is FALSE.

A text string can be changed based on the condition code. When the LINE statement is executed, only the values of the text string will be printed. In the code below, the text string contains a value when the IF statement is TRUE, and another value when the IF statement is FALSE. A LENGTH statement is added to avoid truncation of the text string.

title "Conditional output printed from the LINE statement";

proc report nowd data=sashelp.class;
   column Name Sex Age Height Weight;
   define Name / display format= $8. width=8 left "Name";
   define Sex / display format= $1. width=6 left "Gender";
   define Age / order format=best9. width=9 right "Age";
   define Height / sum format=best9. width=9 right "Height";
   define Weight / sum format=best9. width=9 right "Weight";
   compute after age;
      length text $ 50;
      if age=15 then text='for age=15 only';
      else text=" ";
      if text=' ' then line text $50.;
   endcomp; run;

The output from the code above shows a text string when the IF condition is TRUE and a blank line when the IF condition is FALSE. Even though there is an IF statement prior to the LINE statement, the LINE statement executes every time the COMPUTE block is processed.

If the desired output is to only print a line of output when there are values, then the code above will not work. The only way to produce a null LINE statement is to use a zero format length for the text string. The $VARYING format is used because of the flexibility of changing the length of the format.

title "Conditional LINE statement processing for null lines of output";

proc report nowd data=sashelp.class;
   column Name Sex Age Height Weight;
   define Name / display format= $8. width=8 left "Name";
   define Sex / display format=$1. width=7 left "Gender"; define Age / order format=best9. 
                                                                       width=9 right "Age";
   define Height / sum format=best9. width=9 right "Height";
   define Weight / sum format=best9. width=9 right "Weight";
   compute after age;
      length text $ 50;
      if age=15 then do;
         text='for age=15 only';
         num=50;
      end;
      else do;
         text="";
         num=0;
      end;
      line text $varying. num;
   endcomp;
run;



These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.