The SAS Output Delivery System and reporting techniques

Proc Report - Average if?

Accepted Solution Solved
Reply
Trusted Advisor
Posts: 1,300
Accepted Solution

Proc Report - Average if?

Hi,

Is there a was to compute an average under a certain condition?

For example say I have a modified version of sashelp.class with an additional record where individual has a weight of zero.  I want to count this individual into his category, but exclude him from the calculated mean.  Here is example code:

proc format;

value myfmt

  low-99.999 = 'Light'

  100-high = 'Heavy';

quit;

data class;

input (name sex) ($) age height weight;

output;

do until(done);

  set sashelp.class end=done;

  output;

end;

cards;

FriedEgg M 99 100 0

;

run;

proc report data=class;

column sex weight=status (Sum Mean),weight;

define sex / group;

define status / across format=myfmt. preloadfmt;

define weight / format=comma8.2;

compute after;

  line ' ';

  line 'Total Class Weight: ' weight.sum comma8.2;

endcomp;

run;

                                                          weight          Sum      Mean

                                             sex       Heavy  Light    weight    weight

                                             F             3      6    811.00     90.11

                                             M             6      5  1,089.50     99.05

                                                    Total Class Weight: 1,900.50   

Desired Output:

                                                S                                 

                                                e     Weight          Sum      Mean

                                                x  Heavy  Light    Weight    Weight

                                                F      3      6    811.00     90.11

                                                M      6      5  1,089.50    108.95

                                                   Total Class Weight: 1,900.50 


Accepted Solutions
Solution
‎11-09-2011 03:27 AM
Super User
Posts: 9,681

Proc Report - Average if?

Sure. Of course.

You need to make a weight variable to exclude this obs.

proc format;
value myfmt
  low-99.999 = 'Light'
  100-high = 'Heavy';
quit;

 

 

data class;
input (name sex) ($) age height weight;
flag=0;
output;
do until(done);
  set sashelp.class end=done; flag=1;
  output;
end;
cards;
FriedEgg M 99 100 0
;
run;

 

 

proc report data=class nowd;
column sex weight=status (Sum Mean),weight;
weight flag;
define sex / group;
define status / across format=myfmt. preloadfmt;
define weight / format=comma8.2;
compute after;
  line ' ';
  line 'Total Class Weight: ' weight.sum comma8.2;
endcomp;
run;

Ksharp

View solution in original post


All Replies
Solution
‎11-09-2011 03:27 AM
Super User
Posts: 9,681

Proc Report - Average if?

Sure. Of course.

You need to make a weight variable to exclude this obs.

proc format;
value myfmt
  low-99.999 = 'Light'
  100-high = 'Heavy';
quit;

 

 

data class;
input (name sex) ($) age height weight;
flag=0;
output;
do until(done);
  set sashelp.class end=done; flag=1;
  output;
end;
cards;
FriedEgg M 99 100 0
;
run;

 

 

proc report data=class nowd;
column sex weight=status (Sum Mean),weight;
weight flag;
define sex / group;
define status / across format=myfmt. preloadfmt;
define weight / format=comma8.2;
compute after;
  line ' ';
  line 'Total Class Weight: ' weight.sum comma8.2;
endcomp;
run;

Ksharp

Trusted Advisor
Posts: 1,300

Proc Report - Average if?

Thanks Ksharp,

Reporting procedures are my bane.

Super User
Posts: 9,681

Proc Report - Average if?

En. Yes. There is lots of thing you need to learn in proc report.

Actually I also spend lots of time to learn this proc which is more complicated than I imaged.

But Here. I will Thank Arthur.Carpenter who wrote a book about it  which  makes me to learn  easily.

It is long time not seeing him here.

Ksharp

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 382 views
  • 0 likes
  • 2 in conversation