<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to calculate mean across two variables in SAS? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466683#M119106</link>
    <description>&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can anyone help me to create a data set that contains the average price per year for each commodity?&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Note: I am using SAS 9.4 version&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 31 May 2018 23:36:05 GMT</pubDate>
    <dc:creator>Prat009</dc:creator>
    <dc:date>2018-05-31T23:36:05Z</dc:date>
    <item>
      <title>How to calculate mean across two variables in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466683#M119106</link>
      <description>&lt;P&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can anyone help me to create a data set that contains the average price per year for each commodity?&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Note: I am using SAS 9.4 version&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 May 2018 23:36:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466683#M119106</guid>
      <dc:creator>Prat009</dc:creator>
      <dc:date>2018-05-31T23:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate mean across two variables in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466689#M119109</link>
      <description>&lt;P&gt;Are you after this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 May 2018 23:43:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466689#M119109</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-31T23:43:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate mean across two variables in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466692#M119111</link>
      <description>&lt;P&gt;I believe you've asked for "by year and commodity" so&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; SQL needs a tweak.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PROC MEANS would be an alternative to PROC SQL for this task.&lt;/P&gt;</description>
      <pubDate>Thu, 31 May 2018 23:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466692#M119111</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-05-31T23:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate mean across two variables in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466701#M119118</link>
      <description>&lt;P&gt;These are the two methods you can use.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Using Data Step:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Using Proc Sql:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table three as
select year, commodity, mean(price) as mean_price
from one
group by year,commodity;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Jun 2018 00:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466701#M119118</guid>
      <dc:creator>Srikanth_B</dc:creator>
      <dc:date>2018-06-01T00:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate mean across two variables in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466703#M119120</link>
      <description>Thanks a lot ! Both of your solutions worked.</description>
      <pubDate>Fri, 01 Jun 2018 02:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466703#M119120</guid>
      <dc:creator>Prat009</dc:creator>
      <dc:date>2018-06-01T02:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate mean across two variables in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466704#M119121</link>
      <description>Thanks for your response!</description>
      <pubDate>Fri, 01 Jun 2018 02:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-mean-across-two-variables-in-SAS/m-p/466704#M119121</guid>
      <dc:creator>Prat009</dc:creator>
      <dc:date>2018-06-01T02:23:19Z</dc:date>
    </item>
  </channel>
</rss>

