BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

3 REPLIES 3
Ksharp
Super User

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

FriedEgg
SAS Employee

Thanks Ksharp,

Reporting procedures are my bane.

Ksharp
Super User

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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