BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
miris
Fluorite | Level 6

Hi,

Does anyone could help to create such program?

Example is in attachment.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You should be able to follow the logic to add the total of A and then add in the division if LAST. 

 

SQL is another option

 

proc SQL;

create table want as

select a.X, sum(a*b)/sum(a)

from have

group by x;

quit;

 

Another possibility is proc means since this is similar as using a FREQ or WEIGHT STATEMENT. 

 

proc means data=have sum wgtsum mean;

class x;

var b;

weight a;

run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Assuming you data is already in sas dataset:

 

data want;

 set have;

       by id group;

            retain tot_sum;

           if first.group then tot_sum = x * y;

          else tot_sum = sum(of tot_sum, x * y);

          if last.group then output;

run;

miris
Fluorite | Level 6

Thank you very much,

 

Yes, My data is already in dataset.

 

It is very helpful but it is not exactly what I need.

What I need is (A1*B1+A2*B2+A3*B3) divided by A1+A2+A3

for every group (X1,X2,X3)

 

thank you

Reeza
Super User

You should be able to follow the logic to add the total of A and then add in the division if LAST. 

 

SQL is another option

 

proc SQL;

create table want as

select a.X, sum(a*b)/sum(a)

from have

group by x;

quit;

 

Another possibility is proc means since this is similar as using a FREQ or WEIGHT STATEMENT. 

 

proc means data=have sum wgtsum mean;

class x;

var b;

weight a;

run;

Shmuel
Garnet | Level 18

It uses same logic as before. You just need retain one more total and divide totals at last:

 

data want;

 set have;

       by id group;

            retain tot_sum_mult  tot_sum_x;

           if first.group then do;

                tot_sum_x = x;    

                tot_sum_mult = x * y;

          end; else do;

              tot_sum_x = sum(of tot_sum_x, x);

              tot_sum_mult = sum(of tot_sum_mult, x * y);

         end;

          if last.group then do;

             result = tot_sum_mult / tot_sum_x;

            output;

         end;

        drop tot_sum_mult tot_sum_x;

run;

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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