Help using Base SAS procedures

How to Use an Order Variable in a Compute Block in Proc Report

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

How to Use an Order Variable in a Compute Block in Proc Report

HI there,

I would like to do an action on a particular cell in Proc Report conditional on the value of other variables on that record.  

Using the generic data below for example I would like weight to show red whenever married=1 or ed=2.

 

The "married condition" is partially working in that weight is highlighted red for the first record with married=1.  However it doesnt work for the rest of the records and this seems to be down to the fact that married is an order variable and not repeating.  I do require married to be an ordered variable so I wonder how I can make the compute block look at the actual value here.

 

Secondly the "ed" condition isn't working at all and I can see no reason why.

 

This is probably down to being relatively new to Proc Report but any help much appreciated!

data base;
set sashelp.bweight  (obs=25);
run;

proc report data = base;
col  married boy smoke weight ed;
define married / order;
define boy / order;
define smoke / order; 
define weight / display;
define ed / display;
compute weight;
  if married=1 then  do;
      call define(_col_,"style","style={background=red}");
  end;
  else if ed=2 then do;
  	      call define(_col_,"style","style={background=red}");
  end;
endcomp;

compute after married;
    line ' ';	
endcomp;
compute after boy;
    line ' ';	
endcomp;
run;

Accepted Solutions
Solution
‎02-14-2017 08:55 AM
SAS Super FREQ
Posts: 709

Re: How to Use an Order Variable in a Compute Block in Proc Report

[ Edited ]

Hi

 

Since the value of an order variable is only available in the very first row, you need to put the value aside.

 

The followingcode sample illustrates the basic principle on how to do this.

 

proc report data=sashelp.class;
  column age name sex _dummy;
  define age / order;
  define name / display;
  define sex / display;
  define _dummy / computed;

  compute _dummy / char length=32;
    if missing(age) = 0 then do;
      saveAge = age;
    end;
    _dummy = cats("age=", age, ",saveAge=", saveAge);
  endcomp;
  compute after age;
    line " ";
  endcomp;
run;

I would recommend to add a "dummy" column add the very end to your report. In the compute block of this dummy variable, you have access to all the variables to left in your report.

 

The dummy varibale does not need to be printed, so simply add the NOPRINT option in the define statement.

 

Bruno

View solution in original post


All Replies
Solution
‎02-14-2017 08:55 AM
SAS Super FREQ
Posts: 709

Re: How to Use an Order Variable in a Compute Block in Proc Report

[ Edited ]

Hi

 

Since the value of an order variable is only available in the very first row, you need to put the value aside.

 

The followingcode sample illustrates the basic principle on how to do this.

 

proc report data=sashelp.class;
  column age name sex _dummy;
  define age / order;
  define name / display;
  define sex / display;
  define _dummy / computed;

  compute _dummy / char length=32;
    if missing(age) = 0 then do;
      saveAge = age;
    end;
    _dummy = cats("age=", age, ",saveAge=", saveAge);
  endcomp;
  compute after age;
    line " ";
  endcomp;
run;

I would recommend to add a "dummy" column add the very end to your report. In the compute block of this dummy variable, you have access to all the variables to left in your report.

 

The dummy varibale does not need to be printed, so simply add the NOPRINT option in the define statement.

 

Bruno

Occasional Contributor
Posts: 16

Re: How to Use an Order Variable in a Compute Block in Proc Report

Posted in reply to Bruno_SAS

That works really welly.  I also hadn't realized that the vars used in the compute block need to be to the left of the computed var.  Cheers!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 135 views
  • 0 likes
  • 2 in conversation