BookmarkSubscribeRSS Feed
LisaYIN9309
Obsidian | Level 7

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!

5 REPLIES 5
PGStats
Opal | Level 21

What have you tried so far?

PG
Reeza
Super User

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. 

Shmuel
Garnet | Level 18

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;
LisaYIN9309
Obsidian | Level 7
This is great, thank you.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 1239 views
  • 0 likes
  • 4 in conversation