Sample 37763: Conditionally print a LINE statement in PROC REPORT
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.
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.
Output from the first sample code
Conditional output printed from the LINE statement
Name Gender Age Height Weight
Joyce F 11 51.3 50.5
Thomas M 57.5 85
James M 12 57.3 83
Jane F 59.8 84.5
John M 59 99.5
Louise F 56.3 77
Robert M 64.8 128
Alice F 13 56.5 84
Barbara F 65.3 98
Jeffrey M 62.5 84
Alfred M 14 69 112.5
Carol F 62.8 102.5
Henry M 63.5 102.5
Judy F 64.3 90
Janet F 15 62.5 112.5
Mary F 66.5 112
Ronald M 67 133
William M 66.5 112
for age=15 only
Philip M 16 72 150
Output from the second sample code
Conditional output printed from the LINE statement
Conditional LINE statement processing for null lines of output
Name Gender Age Height Weight
Joyce F 11 51.3 50.5
Thomas M 57.5 85
James M 12 57.3 83
Jane F 59.8 84.5
John M 59 99.5
Louise F 56.3 77
Robert M 64.8 128
Alice F 13 56.5 84
Barbara F 65.3 98
Jeffrey M 62.5 84
Alfred M 14 69 112.5
Carol F 62.8 102.5
Henry M 63.5 102.5
Judy F 64.3 90
Janet F 15 62.5 112.5
Mary F 66.5 112
Ronald M 67 133
William M 66.5 112
for age=15 only
Philip M 16 72 150
There is not a way to conditionally execute a COMPUTE BEFORE and COMPUTE AFTER LINE statement. However, if a text string is used for the LINE statement and the format associated with the text string is 0, then the LINE will not print out.
Date Modified: | 2020-02-12 13:54:20 |
Date Created: | 2009-11-09 12:49:55 |
Operating System and Release Information
SAS System | Base SAS | z/OS | | |
OpenVMS VAX | | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
Microsoft Windows XP 64-bit Edition | | |
Microsoft® Windows® for x64 | | |
OS/2 | | |
Microsoft Windows 7 | | |
Microsoft Windows 95/98 | | |
Microsoft Windows 2000 Advanced Server | | |
Microsoft Windows 2000 Datacenter Server | | |
Microsoft Windows 2000 Server | | |
Microsoft Windows 2000 Professional | | |
Microsoft Windows NT Workstation | | |
Microsoft Windows Server 2003 Datacenter Edition | | |
Microsoft Windows Server 2003 Enterprise Edition | | |
Microsoft Windows Server 2003 Standard Edition | | |
Microsoft Windows Server 2008 | | |
Microsoft Windows XP Professional | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
64-bit Enabled AIX | | |
64-bit Enabled HP-UX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
AIX | | |
HP-UX | | |
HP-UX IPF | | |
IRIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |