<?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 Re: how to calculate mean of two values from another table? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-mean-of-two-values-from-another-table/m-p/391976#M94233</link>
    <description>&lt;P&gt;Thank you so much for replying. This is working.&lt;/P&gt;</description>
    <pubDate>Wed, 30 Aug 2017 18:27:03 GMT</pubDate>
    <dc:creator>C_V</dc:creator>
    <dc:date>2017-08-30T18:27:03Z</dc:date>
    <item>
      <title>how to calculate mean of two values from another table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-mean-of-two-values-from-another-table/m-p/391921#M94218</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have two tables&amp;nbsp;for&amp;nbsp;example&lt;/P&gt;&lt;P&gt;Table_A:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;BEG_PERIOD&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;END_PERIOD&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Value&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1-Apr-12&lt;/TD&gt;&lt;TD&gt;1-Apr-13&lt;/TD&gt;&lt;TD&gt;1,181.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1-Apr-13&lt;/TD&gt;&lt;TD&gt;1-Apr-14&lt;/TD&gt;&lt;TD&gt;1,214.99&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1-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Oct-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1-Oct-17&lt;/TD&gt;&lt;TD&gt;1-Oct-18&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1-Oct-18&lt;/TD&gt;&lt;TD&gt;1-Oct-19&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to add another column in Table_B that calculates the average of the Value column from Table_A based on the periods in Table_B.&lt;/P&gt;&lt;P&gt;So the first value of the new column in Table_B should be the average of 1,256.47 and 1,292.0, the second value should be the average of 1,292.00 and 1,324.30 etc..&lt;/P&gt;&lt;P&gt;I have this code but it didn't work:&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table&amp;nbsp;test as&lt;BR /&gt;select A.*,&lt;BR /&gt;mean(B.Value) as Test&lt;BR /&gt;from Table_B as A left join Table_A as B&lt;BR /&gt;on B.Beg_Period &amp;lt; A.Beg_Period &amp;lt; B.End_Period and&lt;BR /&gt;&amp;nbsp;B.Beg_Period &amp;lt; A.End_Period &amp;lt; B.End_Period;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;Any help would be very much appreciated!!&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2017 16:06:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-mean-of-two-values-from-another-table/m-p/391921#M94218</guid>
      <dc:creator>C_V</dc:creator>
      <dc:date>2017-08-30T16:06:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to calculate mean of two values from another table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-mean-of-two-values-from-another-table/m-p/391944#M94226</link>
      <description>&lt;P&gt;Are your date values SAS dates or character?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the rule to match the results on a 6-month difference&amp;nbsp;this works for the example data. You would have to do the code separately, your choice of methods to get the 2 periods averaged. I put it in the data step to read values since I had to create data steps to have data to test. This is how you should post example data. If I make an assumption, such as your dates should be SAS date values but they aren't then the code will likely not work with your actual data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data table_a;
   informat BEG_PERIOD END_PERIOD anydtdte. Value comma12.;
   format BEG_PERIOD END_PERIOD date9.;
   input BEG_PERIOD END_PERIOD Value ;
   avg = mean(value, lag(value));
   if _n_=1 then call missing(avg);
datalines;
1-Apr-12 1-Apr-13 1,181.00 
1-Apr-13 1-Apr-14 1,214.99 
1-Apr-14 1-Apr-15 1,256.47 
1-Apr-15 1-Apr-16 1,292.00 
1-Apr-16 1-Apr-17 1,324.30 
1-Apr-17 1-Apr-18 1,357.41 
1-Apr-18 1-Apr-19 1,391.34 
1-Apr-19 1-Apr-20 1,426.13 
;
run;
 

data Table_B;
   informat BEG_PERIOD END_PERIOD anydtdte. ;
   format BEG_PERIOD END_PERIOD date9.;
   input BEG_PERIOD END_PERIOD ;
datalines;
1-Oct-14 1-Oct-15 
1-Oct-15 1-Oct-16 
1-Oct-16 1-Oct-17 
1-Oct-17 1-Oct-18 
1-Oct-18 1-Oct-19 
;
run;

proc sql;
   create table want as
   select b.*, a.avg
   from table_b as b 
        left join
        table_a as a
        on b.end_period = intnx('month',a.beg_period,6,'same')
   ;
quit;
 
&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Aug 2017 17:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-mean-of-two-values-from-another-table/m-p/391944#M94226</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-30T17:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to calculate mean of two values from another table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-mean-of-two-values-from-another-table/m-p/391976#M94233</link>
      <description>&lt;P&gt;Thank you so much for replying. This is working.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2017 18:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-mean-of-two-values-from-another-table/m-p/391976#M94233</guid>
      <dc:creator>C_V</dc:creator>
      <dc:date>2017-08-30T18:27:03Z</dc:date>
    </item>
  </channel>
</rss>

