Help using Base SAS procedures

calculating average while using if then else clause

Reply
Contributor
Posts: 34

calculating average while using if then else clause

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

Grand Advisor
Posts: 16,842

Re: calculating average while using if then else clause

Show your proc means code.

Contributor
Posts: 34

Re: calculating average while using if then else clause

proc means data=abc mean;

  class Rule /mlf order=data;

  types rule;

  var score;

run;

Esteemed Advisor
Posts: 5,155

Re: calculating average while using if then else clause

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
Grand Advisor
Posts: 16,842

Re: calculating average while using if then else clause

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;

Contributor
Posts: 34

Re: calculating average while using if then else clause

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;

Grand Advisor
Posts: 16,842

Re: calculating average while using if then else clause

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).

Contributor
Posts: 34

Re: calculating average while using if then else clause

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.


Grand Advisor
Posts: 16,842

Re: calculating average while using if then else clause

Use 1 variable with 35 levels instead.

Contributor
Posts: 34

Re: calculating average while using if then else clause

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.  

Grand Advisor
Posts: 16,842

Re: calculating average while using if then else clause

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.

SAS Employee
Posts: 1

Re: calculating average while using if then else clause

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.

Ask a Question
Discussion stats
  • 11 replies
  • 351 views
  • 3 likes
  • 4 in conversation