turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- calculating average while using if then else claus...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-22-2015 07:48 PM

I applied a set of rules to eliminate records using if then else. so now i want to calculate average of the selected ones for each rule. when im using proc meant for those rules it is giving me only the average of the eliminated ones but not selected ones.

it looks something like this

data abc;

set xyz;

if abc<9000 then rule1=1;

else if xu=1 then rule2=1;

else selected =1;

run;

thank you

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-22-2015 11:58 PM

Show your proc means code.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-23-2015 03:17 PM

proc means data=abc mean;

class Rule /mlf order=data;

types rule;

var score;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-23-2015 04:06 PM

Been a while since I dug into PROC MEANS, but I can't see that you sample code makes sense.

Your data step produces *rule1* and *rule2* variables, but you refer to *rule* in means.

Without knowing what you try to achieve, perhaps selected could be rule = 3...? Or run means twice?

Data never sleeps

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-23-2015 04:32 PM

If I understand your question, which I'm not sure I do, you're creating 3 groups and using 3 variables to do so.

Use one variable with three different levels instead and use that in your class statement.

data abc;

set xyz;

if abc<9000 then rule=1;

else if xu=1 then rule=1;

else rule =3;

run;

proc means data=abc mean;

class Rule;

var score;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-23-2015 05:15 PM

Sorry my mistake this is how my proc means look like:

proc means data=exclusion_selected mean;

Class rule1 rule2 selected/missing;

types rule1 rule2 selected;

var score;

run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-23-2015 05:50 PM

Did you try the code I suggested?

Otherwise please provide sample input/output data for what your looking for. Your code is correct, syntax wise, so not sure why your not getting what you want (mostly because we don't know what you want).

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2015 08:53 PM

data abc;

set xyz;

if abc<9000 then rule1=1;

else if xu=1 then rule2=1;

else if age <18 then rule3=1;

else if age >65 then rule4=1;

else selected =1;

run;

now i need to calculate Average of the Score. For example:

Data rule1;

Set abc;

if rule1=1 then delete;

run;

proc means data=rule1 mean maxdec=2;

var score;

run;

data rule2;

set rule1;

if rule2=1 then delete;

run;

proc means data=rule2 mean maxdec=2;

var score;

run;

Since I have 35 rules, I want to know alternative way to calculate the average of only selected observation of that particular rule.

Thank you.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2015 08:58 PM

Use 1 variable with 35 levels instead.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2015 09:11 PM

No, that wont work . For example lets say I have population of 100,000 and when applied rule1 it eliminates about 25,000, Now I need to calculate average of 75,000 and apply rule2 on the selected ones (remaining 75,000) if it eliminates about 10,000 then I need to calculate average of remaining 65,000 and so on.

In your case it will calculate average for the ones which are eliminated.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2015 11:39 PM

Ok, then I guess I don't understand. Good luck!

EDIT:

Initialize your rule variables to 0/1 rather than 1/missing.

You can use an array statement to do this:

array rule(36) rule1-rule35 selected {36*0};

Then try your proc means again. You can probably use a WAYS 1 rather than list everything in your TYPES statement.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2015 10:14 PM

Assuming you use one(1) variable with 35 levels.

for example,

data abc;

set xyz;

if abc<9000 then rule=1;

else if xu=1 then rule=2;

....(and so on).......

else rule =35;

run;

You can use the WHERE option to eliminate every level.

Proc means data = abc (where = (rule GT 1)) mean maxdec = 2;

var score;

run;

Proc means data = abc (where = (rule GT 2)) mean maxdec = 2;

var score;

run;

and so on

Unfortunately, you'll have 35 proc means.