Monday, 21 March 2016

SAS: CONDITIONALLY EXECUTING A LINE STATEMENT IN PROC REPORT

By design, PROC REPORT always prints a LINE statement. However, when the format for the entire LINE statement is 0, the LINE statement is created in memory but does not print in the corresponding output. The SAS format $VARYINGw. length-variable is used in this technique because the value for length-variable can be changed each time the COMPUTE block is executed.
In the following example code, an IF statement is used to determine when AGE=15.

ods html ;
proc report nowd data=sashelp.class style(column header)=[background=white  rules=none];

 column Name Sex Age Height Weight;

 where age in(15,16);

 define Name / display format= $8. "Name" ;
 define Sex / display format= $1. "Sex" ;
 define Age / order format= best9. "Age" width=5;
 define Height / sum format= best9. "Height" ;
 define Weight / sum format= best9. "Weight" ;

 compute after age;
 if age=15 then do;
 text = 'age=15 only';
 num=50;
 end;

 else do;
 text = "";
 num=0;
 end;

 line text $varying. num ;
 endcomp;

run;
ods html close;


When the IF condition is true, a DATA step variable is created to hold the LINE statement text and length-variable is set to an appropriate length for the text to print. When the IF statement is false, the DATA step variable is set to blank and length-variable is set to 0. The LINE statement is used to print the value of the DATA step variable using the format $VARYINGw. length-variable. The resulting output is shown in Figure 1.
Figure 1. ODS HTML Showing a LINE Statement that Appears to Be Conditional

No comments:

Post a Comment