## calculating averages of a variable depending on another variable and grouping them

Solved
Super Contributor
Posts: 454

# calculating averages of a variable depending on another variable and grouping them

Hi,

suppose I have the following table:

stockdatecategorysize
A31/12/2010130
A31/12/2010020
A31/12/2010140
A27/11/201104
A27/11/201116
A27/11/201118

for each stock-date combination, I would like to get the total average size and the average size of category 1:

stockdatetotal average sizecat1 average size
A31/12/20103035
A27/11/201167

Thank you!

Accepted Solutions
Solution
‎05-14-2015 08:52 AM
Super User
Posts: 10,395

## Re: calculating averages of a variable depending on another variable and grouping them

```data have;
infile cards expandtabs truncover;
input stock \$ date : ddmmyy10. cat size;
format date ddmmyy10.;
cards;
A     31/12/2010     1     30
A     31/12/2010     0     20
A     31/12/2010     1     40
A     27/11/2011     0     4
A     27/11/2011     1     6
A     27/11/2011     1     8
;
run;

proc sql;
create table want as
select stock,date,mean(size) as mean_all,
(select mean(size) from have where stock=a.stock and date=a.date and cat=1) as mean_1_cat
from have as a
group by stock,date;
quit;
```

Xia Keshan

All Replies
Super User
Posts: 21,936

## Re: calculating averages of a variable depending on another variable and grouping them

Not quite the same format but try a proc means looking at WAYS and TYPE statement.

You could do a single proc sql as well.

Occasional Contributor
Posts: 15

## Re: calculating averages of a variable depending on another variable and grouping them

Code:-

proc sql;

create table sample1 as

select stock,date,  avg(size) as total_average_size

from stock

group by date;

quit;

proc sql;

 create table sample2 as

select stock,date, category,avg(size) as cat1_avg_size

from stock

where category=1

group by date, category;

quit;

proc sql;

create table final as

select a.*, b.cat1_avg_size

from sample1 a, sample2 b

where a.stock=b.stock;

quit;

Valued Guide
Posts: 863

## Re: calculating averages of a variable depending on another variable and grouping them

data have;

infile cards dsd;

informat date ddmmyy10.;

format date ddmmyy10.;

input stock \$ date category    size;

cards;

A,31/12/2010,1,30

A,31/12/2010,0,20

A,31/12/2010,1,40

A,27/11/2011,0,4

A,27/11/2011,1,6

A,27/11/2011,1,8

;

run;

proc sql;

create table want as

select distinct stock,avg_all,date,avg(size) as avg_one

from(

select distinct *,avg(size) as avg_all

from have

group by stock,date)

where category = 1

group by stock,date;

Solution
‎05-14-2015 08:52 AM
Super User
Posts: 10,395

## Re: calculating averages of a variable depending on another variable and grouping them

```data have;
infile cards expandtabs truncover;
input stock \$ date : ddmmyy10. cat size;
format date ddmmyy10.;
cards;
A     31/12/2010     1     30
A     31/12/2010     0     20
A     31/12/2010     1     40
A     27/11/2011     0     4
A     27/11/2011     1     6
A     27/11/2011     1     8
;
run;

proc sql;
create table want as
select stock,date,mean(size) as mean_all,
(select mean(size) from have where stock=a.stock and date=a.date and cat=1) as mean_1_cat
from have as a
group by stock,date;
quit;
```

Xia Keshan

🔒 This topic is solved and locked.

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

Discussion stats
• 4 replies
• 307 views
• 0 likes
• 5 in conversation