Solved
Valued Guide
Posts: 617

# Compute with If Condition

Is there a way to do:

compute after gender but not after age  / style={background=blue};

line ' ';

endcomp;

Accepted Solutions
Solution
‎04-07-2016 02:27 PM
Posts: 3,852

## Re: Compute with If Condition

This might be helpful but seems a bit clunky.

proc sort  data=sashelp.class out=class;
by sex age;
run;
data report;
set class;
by sex age;
lastsex = last.sex;
lastage = last.age;
run;
proc report nowd data=report list out=test;
columns sex lastsex age lastage name height weight;
define sex / order;
define age / order;
define lastsex / order noprint;
define lastage / order noprint;
define name / display;
define height / display;
define weight / display;
compute after lastage / style={background=lightblue};
x = ' ';
if lastsex eq 1 then lastage=0;
line x \$varying1. lastage;
endcomp;
compute after sex / style={background=blue};
line ' ';
endcomp;
run;

All Replies
Super User
Posts: 9,599

## Re: Compute with If Condition

Well, there maybe in report, but I prefer pre-arranging data in a datastep, som my suggestion would be to add a break var into your dataset, i.e. its always 0, except when you line, then break on that.

Posts: 3,852

## Re: Compute with If Condition

I don't quite understand your example.  If you want a conditional line statement use \$VARYING format as in the sample.

http://support.sas.com/kb/37/763.html

Valued Guide
Posts: 617

## Re: Compute with If Condition

See screen shot.  What I’m trying to do is only produce one line when the blue line is created rather than both the blue line and the light blue line.

compute after gender / style={background=blue};
line ' ';
endcomp;

compute after age / style={background=lightblue};
line ' ';
endcomp;

SAS Super FREQ
Posts: 9,365

## Re: Compute with If Condition

Which line do you want between Female and Male??? The light blue line or the dark blue line?

The technique described (to use \$varying. is the correct technique to suppress a line statement. You CANNOT execute a LINE statement conditionally, but you can conditionally assign a value to a "helper" variable that specifies the length of the line. When the length of the line is 0, then PROC REPORT will suppress the line.

Please see this Tech Support note: http://support.sas.com/kb/37/763.html which outlines the basic technique (illustrated with writing out text). And this paper http://support.sas.com/resources/papers/proceedings11/246-2011.pdf has more examples and explanation on pages 12 and 13.

cynthia

Valued Guide
Posts: 617

## Re: Compute with If Condition

[ Edited ]

I want the last light blue line to not appear.  Currently playing with

if residency_desc = "Total" then do;

call define(_col_, 'style', 'style=[background=lightblue]');

line ' ';

end;

else do;

call define(_col_, 'style', 'style=[background=blue]');

line ' ';

end;

But this produces two blank lines.

Solution
‎04-07-2016 02:27 PM
Posts: 3,852

## Re: Compute with If Condition

This might be helpful but seems a bit clunky.

proc sort  data=sashelp.class out=class;
by sex age;
run;
data report;
set class;
by sex age;
lastsex = last.sex;
lastage = last.age;
run;
proc report nowd data=report list out=test;
columns sex lastsex age lastage name height weight;
define sex / order;
define age / order;
define lastsex / order noprint;
define lastage / order noprint;
define name / display;
define height / display;
define weight / display;
compute after lastage / style={background=lightblue};
x = ' ';
if lastsex eq 1 then lastage=0;
line x \$varying1. lastage;
endcomp;
compute after sex / style={background=blue};
line ' ';
endcomp;
run;

Valued Guide
Posts: 617

## Re: Compute with If Condition

if lastsex eq 1 then lastage=0;
line x \$varying1. lastage;

Could you explain what these two lines do?  They seem to matter why is lastage being set to 0?

Posts: 3,852

## Re: Compute with If Condition

DavidPhillips2 wrote:
if lastsex eq 1 then lastage=0;
line x \$varying1. lastage;

Could you explain what these two lines do?  They seem to matter why is lastage being set to 0?

Lastage is both the indicator of last obs for an age group and used as the length for X in \$VARYING.  When LASTAGE is 0 it keeps the compute from printing a light blue line.  Otherwise there would be a line between each row.  When it is LASTSEX it is also LASTAGE but we want to suppress the printing of that line so set it (LASTAGE) to 0 and \$VARYING prints nothing.

Valued Guide
Posts: 617

## Re: Compute with If Condition

[ Edited ]

When I run this with my dataset and a third column I run into:

NOTE: Variable lastsex is uninitialized.ERROR: An unknown, abnormal error has occurred. This step is being terminated.

It’s hard to replicate my scenario via a post.  Working on a simplification so I can post code and data.

This block is causing the error for me.

compute after lastage / style={background=lightblue};
x = ' ';
if lastsex eq 1 then lastage=0;
line x \$varying1. lastage;
endcomp;
Posts: 3,852

## Re: Compute with If Condition

DavidPhillips2 wrote:

When I run this with my dataset and a third column I run into:

NOTE: Variable lastsex is uninitialized.ERROR: An unknown, abnormal error has occurred. This step is being terminated.

It’s hard to replicate my scenario via a post.  Working on a simplification so I can post code and data.

This block is causing the error for me.

compute after lastage / style={background=lightblue};
x = ' ';
if lastsex eq 1 then lastage=0;
line x \$varying1. lastage;
endcomp;

You need to create "last" indicator variables for your data as I did in this step.  They also go in the column statement.

data report;
set class;
by sex age;
lastsex = last.sex;
lastage = last.age;
run;
Valued Guide
Posts: 617

## Re: Compute with If Condition

[ Edited ]

This is a bit simplified.  Still simplifying and working on getting data for the post.

proc sort data=all;

by gender residency eth;

run;

data all; set all;

by gender residency eth;

lastGender = last.gender;

lastResidency = last.residency;

lastEth = last.eth;

run;

proc report data=all;

column (gender residency eth growthDeclineColumn perGrowthDeclineColumn lastGender lastResidency lastEth     academic_period_desc , n);

define gender / group  '' order=data;

define residency / group  '' order=data;

define eth / group '' order=internal;

define lastEth / order=data;

define lastResidency / order=data;

define lastGender / order=data;

define growthDeclineColumn                      / group noprint '' order = data;

define perGrowthDeclineColumn              / group noprint '' order = data;

define n                                   / analysis sum '' &tabulateFormatClause;

compute eth;

if eth=99  then call define(_row_,'style','style={font_weight=bold}');

endcomp;

compute n;

if growthDeclineColumn = 1 then call &shortFormat;

if perGrowthDeclineColumn = 1 then call define(_col_,'format','percent8.2');

endcomp;

compute after residency / style=[background=blue];

x = ' ';

if lastGender eq 1 then lastResidency =0;

line x \$varying1. lastResidency;

endcomp;

Valued Guide
Posts: 617

## Re: Compute with If Condition

if lastReport1 eq 1 or lastReport1 eq .  then lastReport2 =0;

makes the proc report show without coloring.  Oddly proc print of dataset all displays lastReport1 with values.  I wonder if this has to do with the after statements.

Posts: 3,852

## Re: Compute with If Condition

[ Edited ]

You need to move the LAST indicator variables to the LEFT in the column statement.  Notice in my example they are not at the end.

Valued Guide
Posts: 617