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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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