Conditional line statement in proc report

Introduction

Sometimes people ask if it is possible to have conditional "line" statements in "proc report". They want to display a line sometimes and at other times not. They soon realise that conditional line statements are not possible using normal methods. But what they are trying to do is possible using the $varying format as will be shown below.

The problem

Firstly, here is a line statement in a compute block that is not conditional.
.
options nocenter;

proc sort data=sashelp.class out=class;
by sex;

proc report data=class nowd;
columns sex name age;
define sex / order width=3 spacing=0;
define name / display;
define age / display;
compute after sex;
  line @1 "------------------------";
endcomp;
run;


 
Sex  Name            Age
F    Alice            13
     Barbara          13
     Carol            14
     Jane             12
     Janet            15
     Joyce            11
     Judy             14
     Louise           12
     Mary             15
------------------------
M    Alfred           14
     Henry            14
     James            12
     Jeffrey          13
     John             12
     Philip           16
     Robert           12
     Ronald           15
     Thomas           11
     William          15
------------------------

Now we try to make it conditional so we get no line after the "F" sex.
 
proc report data=class nowd;
columns sex name age;
define sex / order width=3 spacing=0;
define name / display;
define age / display;
compute after sex;
  if sex="M" then do;
    line @1 "------------------------";
  end;
endcomp;
run;

The output is exactly the same. We have a dashed line under both sexes.
 
Sex  Name            Age
F    Alice            13
     Barbara          13
     Carol            14
     Jane             12
     Janet            15
     Joyce            11
     Judy             14
     Louise           12
     Mary             15
------------------------
M    Alfred           14
     Henry            14
     James            12
     Jeffrey          13
     John             12
     Philip           16
     Robert           12
     Ronald           15
     Thomas           11
     William          15
------------------------

And what does it say in the log to tell us we can not do this? I'll show you.
 
276  proc report data=class nowd;
277  columns sex name age;
278  define sex / order width=3 spacing=0;
279  define name / display;
280  define age / display;
281  compute after sex;
282    if sex="M" then do;
283      line @1 "------------------------";
284    end;
285  endcomp;
286  run;

NOTE: There were 19 observations read from the data set WORK.CLASS.
NOTE: PROCEDURE REPORT used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

 

There is nothing in the log to tell us something is wrong, even though it has not worked in the way we expected.

We can change what gets displayed by the line statement and show nothing for sex="F" but we can't get rid of the line using this method.
 
proc report data=class nowd;
columns sex name age;
define sex / order width=3 spacing=0;
define name / display;
define age / display;
compute after sex;
  msg="------------------------";
  if sex="F" then msg="";
  line @1 msg $24.;
endcomp;
run;

 
Sex  Name            Age
F    Alice            13
     Barbara          13
     Carol            14
     Jane             12
     Janet            15
     Joyce            11
     Judy             14
     Louise           12
     Mary             15

M    Alfred           14
     Henry            14
     James            12
     Jeffrey          13
     John             12
     Philip           16
     Robert           12
     Ronald           15
     Thomas           11
     William          15
------------------------
 

The solution

The solution is to use the $varying format and to suppress the output of the line by using a length of 0. (I won't explain the $varying format. You can look this up in the sas documentation.)
 
proc report data=class nowd;
columns sex name age;
define sex / order width=3 spacing=0;
define name / display;
define age / display;
compute after sex;
  msg="------------------------";
  if sex="F" then len=0;
  else len=24;
  line @1 msg $varying24. len;
endcomp;
run;

Now we get what we want.
 
Sex  Name            Age
F    Alice            13
     Barbara          13
     Carol            14
     Jane             12
     Janet            15
     Joyce            11
     Judy             14
     Louise           12
     Mary             15
M    Alfred           14
     Henry            14
     James            12
     Jeffrey          13
     John             12
     Philip           16
     Robert           12
     Ronald           15
     Thomas           11
     William          15
------------------------

Compute after _page_ lines

Sometimes you want to draw a line at the end of each page using "compute after _page_" but you would like the line on the very last page to be directly under the data rather than at the bottom of the page. You can do this using this method.
 
options nocenter ps=40 ;
 
proc report data=sasuser.adv headline headskip nowd;
  columns patno amsoc;
  define patno / display;
  define amsoc / display;
  compute before;
    len=71;
    len2=len;
    text=repeat("_",135);
  endcomp;
  compute after _page_;
    line @3 text $varying. len;
  endcomp;
  compute after;
    len=0;
    line @3 text $varying. len2;
  endcomp;
run;

Conclusion

You can seen a way to effectively make the "line" statement in a proc report "compute" block conditional by using the $varying format.
 


 
 

Go back to the home page.

E-mail the macro and web site author.