summing across 2 variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

summing across 2 variables

I need to sum variables 'Inc' and 'Frames' by Seq(Interval) such that what used to be >1 observation becomes only one per 'Int' per 'Seq.'  An example follows:

Have:

Seq     Int    Inc     Frames

A          1     10     60    

A          1     16     60 

A          2     36     41

A          2     23     23

A          3     46     60 

A          3     53     60

B          1     28     60

B          1     34     60

B          1     14     60

B          2     32     32

B          2     19     23

B          2     59     68

B          3     23     60

B          3     36     60

B          3     42     60

C          1     23     60

C          2     10     11

C          3     57     60

Need the sums of the above Inc and Frame variables.

Seq     Int    Inc     Frames     Inc/Frames

A          1     26     120          =(26/120)

A          2     59     64            =(59/64)  etc....

A          3     99     120

B          1     76     120

B          2     110     123

B          3     101     120

C          1     23     120

C          2     10     11

C          3     57     120

I greatly appreciate the help.


Accepted Solutions
Solution
‎06-04-2012 07:20 PM
Super Contributor
Posts: 1,636

Re: summing across 2 variables

How about:

data have;

input Seq :$     Int   : Inc   :  Frames;

cards;

A          1     10     60   

A          1     16     60

A          2     36     41

A          2     23     23

A          3     46     60

A          3     53     60

B          1     28     60

B          1     34     60

B          1     14     60

B          2     32     32

B          2     19     23

B          2     59     68

B          3     23     60

B          3     36     60

B          3     42     60

C          1     23     60

C          2     10     11

C          3     57     60

;

proc sql;

create table want as

  select seq,int,sum(inc) as inc,sum(frames) as frames,calculated inc/calculated frames as new_variable

    from have

  group by seq,int

    order by 1,2;

quit;

proc print;run;

Linlin

View solution in original post


All Replies
Solution
‎06-04-2012 07:20 PM
Super Contributor
Posts: 1,636

Re: summing across 2 variables

How about:

data have;

input Seq :$     Int   : Inc   :  Frames;

cards;

A          1     10     60   

A          1     16     60

A          2     36     41

A          2     23     23

A          3     46     60

A          3     53     60

B          1     28     60

B          1     34     60

B          1     14     60

B          2     32     32

B          2     19     23

B          2     59     68

B          3     23     60

B          3     36     60

B          3     42     60

C          1     23     60

C          2     10     11

C          3     57     60

;

proc sql;

create table want as

  select seq,int,sum(inc) as inc,sum(frames) as frames,calculated inc/calculated frames as new_variable

    from have

  group by seq,int

    order by 1,2;

quit;

proc print;run;

Linlin

Respected Advisor
Posts: 3,777

Re: summing across 2 variables

Like this?

proc summary data=test nway;

   class seq int;

   output out=sum(drop=_Smiley Happy sum(Inc Frames)=;

   run;

data sum;

   set sum;

   Inc_frm=inc/Frames;

   run;

Proc print;

   run;

Occasional Contributor
Posts: 10

Re: summing across 2 variables

*Many thanks to you both-- either way works perfectly, nice job

SAS Super FREQ
Posts: 8,743

Re: summing across 2 variables

Hi:

And yet another solution would be to use PROC REPORT -- no PROC SQL needed. (Data set name is work.mydata.)

cynthia

ods html file='c:\temp\proc_report_with_calcvar.html';

proc report data=mydata nowd;

  column seq int inc frames calcvar;

  define seq / group;

  define int/group;

  define inc / sum;

  define frames/ sum;

  define calcvar / computed;

  compute calcvar;

    calcvar = inc.sum / frames.sum;

  endcomp;

run;

ods html close;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 399 views
  • 3 likes
  • 4 in conversation