DATA Step, Macro, Functions and more

How to average up several rows of different group with condition

Reply
Contributor
Posts: 36

How to average up several rows of different group with condition

I've come across this problem that I want to average up rows that have exact same OrganizationID, Product and ProductLine, make it one row, with summed numerator as new numerator, summed denominator as new denominator, and summed numerator/summed denominator as new rate(AggregareRate).

 

The average up will only happen if all three column values are the same(OrganizationID, Product and ProductLine). So in the example below, when either product or productline have different values under same organizationID, the rows stay the way it is - only red rows got averaged up.

 

Here is the input data:

OrganizationIDProductProductLinenumeratordenominatorrate
1ShoesCommercial502000.25
1ShoesCommercial601200.5
1ShoesNon-Commercial30500.6
2ShoesCommercial801600.5
2ShoesNon-Commericial901800.5
2ClothCommercial701000.7
2ClothCommercial603000.2

 

The desired output should look like:

OrganizationIDProductProductLinenumeratordenominatorAggregateRate
1ShoesCommercial1103200.34375
1ShoesNon-Commercial30500.6
2ShoesCommercial801600.5
2ShoesNon-Commericial901800.5
2ClothCommercial1304000.325

 

Thank you!

Esteemed Advisor
Posts: 5,398

Re: How to average up several rows of different group with condition

Posted in reply to LisaYIN9309

What have you tried so far?

PG
Super User
Posts: 22,846

Re: How to average up several rows of different group with condition

[ Edited ]
Posted in reply to LisaYIN9309

PROC MEANS, SQL, SUMMARY, UNIVARIATE, or DATA STEP. 

 

Check the PROC MEANS examples for one related to your question. 

 

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic

 

If youre having difficulties with a specific issue from here post your code and log. 

Trusted Advisor
Posts: 1,826

Re: How to average up several rows of different group with condition

There are many tools to reach the desired output, as @Reeza mentiond.

You can do it in one step (datastep) or in two steps (proc means or proc summary + datastep).

 

Attached is the code for one datastep, assuming data is already sorted

        BY OrganizationID Product and ProductLine;

 

data want;
 set have;
   by OrganizationID Product ProductLine;
        retain sum_num sum_denom;
       if first.ProductLine then do; 
            sum_num = nemurator;
            sum_denom = denominator;
       end; else do;
            sum_num =sum(sum_num nemurator);
            sum_denom = sum(sum_denom denominator);
       end;

       if last.ProductLine then do; 
          AggregateRate = sum_num / sum_denom;
          output;
      end;
format AggregateRate 8.5; /* or any other preffered format */ run;
Contributor
Posts: 36

Re: How to average up several rows of different group with condition

This is great, thank you.
Contributor
Posts: 36

Re: How to average up several rows of different group with condition

Thank you for this!
Ask a Question
Discussion stats
  • 5 replies
  • 129 views
  • 0 likes
  • 4 in conversation