Fluorite | Level 6

## How to calculate mean across two variables in SAS?

I have a data set which contains data on the average price of unleaded regular gasoline (per gallon), whole large eggs (per dozen), and whole milk (per gallon). The variables in this file are year, month, price, and type of commodity.

``````Year Month  Price   Commodity
2004    1   1.592   Gas
2004    2   1.672   Gas
2005    1   1.766   Gas
2005    2   1.833   Gas
2006    1   2.009   Gas
2006    2   2.041   Gas
2004    1   1.95    Egg
2004    2   1.979   Egg
2005    1   1.97    Egg
2005    2   1.951   Egg
2006    1   2.032   Egg
2006    2   2.21    Egg
2004    1   2.879   Milk
2004    2   2.814   Milk
2005    1   2.786   Milk
2005    2   2.906   Milk
2006    1   3.374   Milk
2006    2   3.574   Milk``````

Can anyone help me to create a data set that contains the average price per year for each commodity?

I am able to create a data set that contains the average price per year or per commodity, but unable to calculate average price per year for each commodity.

Note: I am using SAS 9.4 version

1 ACCEPTED SOLUTION

Accepted Solutions
Calcite | Level 5

## Re: How to calculate mean across two variables in SAS?

These are the two methods you can use.

``````Data one;
input year month price commodity \$;
Datalines;
2004 1 1.592 Gas
2004 2 1.672 Gas
2005 1 1.766 Gas
2005 2 1.833 Gas
2006 1 2.009 Gas
2006 2 2.041 Gas
2004 1 1.95 Egg
2004 2 1.979 Egg
2005 1 1.97 Egg
2005 2 1.951 Egg
2006 1 2.032 Egg
2006 2 2.21 Egg
2004 1 2.879 Milk
2004 2 2.814 Milk
2005 1 2.786 Milk
2005 2 2.906 Milk
2006 1 3.374 Milk
2006 2 3.574 Milk
;
run;``````

Using Data Step:

``````proc sort data=one;
by year commodity;
run;

Proc means data=one noprint;
var price;
by year commodity;
output out=two(drop=_type_ _freq_) mean=;
run;``` ```

Using Proc Sql:

``````proc sql;
create table three as
select year, commodity, mean(price) as mean_price
from one
group by year,commodity;
quit;``````
5 REPLIES 5
Tourmaline | Level 20

## Re: How to calculate mean across two variables in SAS?

Are you after this?

``````data have;
input Year Month  Price   Commodity \$;
cards;
2004    1   1.592   Gas
2004    2   1.672   Gas
2005    1   1.766   Gas
2005    2   1.833   Gas
2006    1   2.009   Gas
2006    2   2.041   Gas
2004    1   1.95    Egg
2004    2   1.979   Egg
2005    1   1.97    Egg
2005    2   1.951   Egg
2006    1   2.032   Egg
2006    2   2.21    Egg
2004    1   2.879   Milk
2004    2   2.814   Milk
2005    1   2.786   Milk
2005    2   2.906   Milk
2006    1   3.374   Milk
2006    2   3.574   Milk
;
proc sql;
create table want as
select commodity, mean(price) as avg_price
from have
group by commodity;
quit;``````
Opal | Level 21

## Re: How to calculate mean across two variables in SAS?

I believe you've asked for "by year and commodity" so @novinosrin SQL needs a tweak.

``````proc sql;
create table want as
/*    select *, mean(price) as avg_price*/
select year, commodity, mean(price) as avg_price
from have
group by year, commodity
;
quit;``````

PROC MEANS would be an alternative to PROC SQL for this task.

Fluorite | Level 6

## Re: How to calculate mean across two variables in SAS?

Calcite | Level 5

## Re: How to calculate mean across two variables in SAS?

These are the two methods you can use.

``````Data one;
input year month price commodity \$;
Datalines;
2004 1 1.592 Gas
2004 2 1.672 Gas
2005 1 1.766 Gas
2005 2 1.833 Gas
2006 1 2.009 Gas
2006 2 2.041 Gas
2004 1 1.95 Egg
2004 2 1.979 Egg
2005 1 1.97 Egg
2005 2 1.951 Egg
2006 1 2.032 Egg
2006 2 2.21 Egg
2004 1 2.879 Milk
2004 2 2.814 Milk
2005 1 2.786 Milk
2005 2 2.906 Milk
2006 1 3.374 Milk
2006 2 3.574 Milk
;
run;``````

Using Data Step:

``````proc sort data=one;
by year commodity;
run;

Proc means data=one noprint;
var price;
by year commodity;
output out=two(drop=_type_ _freq_) mean=;
run;``` ```

Using Proc Sql:

``````proc sql;
create table three as
select year, commodity, mean(price) as mean_price
from one
group by year,commodity;
quit;``````
Fluorite | Level 6

## Re: How to calculate mean across two variables in SAS?

Thanks a lot ! Both of your solutions worked.
Discussion stats
• 5 replies
• 5600 views
• 2 likes
• 4 in conversation