<?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 lag by region, month, &amp;amp; year? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-lag-by-region-month-amp-year/m-p/256937#M57192</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am attempting to lag a variable by region, month, and year. &amp;nbsp;Essentially, I need to create a new variable that is the lagged mean value (by year and month) for a neighboring region.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this:&lt;/P&gt;
&lt;P&gt;region month year mean&lt;/P&gt;
&lt;P&gt;1 1 00 5&lt;/P&gt;
&lt;P&gt;1 2 00 6&lt;/P&gt;
&lt;P&gt;1 3 00 4&lt;/P&gt;
&lt;P&gt;1 1 01 4&lt;/P&gt;
&lt;P&gt;1 2 01 5&lt;/P&gt;
&lt;P&gt;1 3 01 5&lt;/P&gt;
&lt;P&gt;2 1 00 4&lt;/P&gt;
&lt;P&gt;2 2 00 5&lt;/P&gt;
&lt;P&gt;2 3 00 3&lt;/P&gt;
&lt;P&gt;2 1 01 3&lt;/P&gt;
&lt;P&gt;2 2 01 5&lt;/P&gt;
&lt;P&gt;2 3 01 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and I need:&lt;/P&gt;
&lt;P&gt;region month year mean neighbor_mean_lag&lt;/P&gt;
&lt;P&gt;1 1 00 5 .&lt;/P&gt;
&lt;P&gt;1 2 00 6 .&lt;/P&gt;
&lt;P&gt;1 3 00 4 .&lt;/P&gt;
&lt;P&gt;1 1 01 4 10&lt;/P&gt;
&lt;P&gt;1 2 01 5 13&lt;/P&gt;
&lt;P&gt;1 3 01 5 16&lt;/P&gt;
&lt;P&gt;2 1 00 10 .&lt;/P&gt;
&lt;P&gt;2 2 00 13 .&lt;/P&gt;
&lt;P&gt;2 3 00 16 .&lt;/P&gt;
&lt;P&gt;2 1 01 3 .&lt;/P&gt;
&lt;P&gt;2 2 01 5 .&lt;/P&gt;
&lt;P&gt;2 3 01 2 .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Note neighbor_mean_lag equals the neighboring region's monthly values for the previous year.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am able to create a lagged variable for the same region using the below code. However, I haven't been successful at creating the lagged variable for a neighboring region.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA lag3;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;SET lag2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; BY region month year;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; lag1_mean=lag(mean);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if first.month and first.year then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lag1_mean=.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end; &lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
    <pubDate>Wed, 16 Mar 2016 01:11:42 GMT</pubDate>
    <dc:creator>jlajla</dc:creator>
    <dc:date>2016-03-16T01:11:42Z</dc:date>
    <item>
      <title>How to lag by region, month, &amp; year?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-lag-by-region-month-amp-year/m-p/256937#M57192</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am attempting to lag a variable by region, month, and year. &amp;nbsp;Essentially, I need to create a new variable that is the lagged mean value (by year and month) for a neighboring region.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this:&lt;/P&gt;
&lt;P&gt;region month year mean&lt;/P&gt;
&lt;P&gt;1 1 00 5&lt;/P&gt;
&lt;P&gt;1 2 00 6&lt;/P&gt;
&lt;P&gt;1 3 00 4&lt;/P&gt;
&lt;P&gt;1 1 01 4&lt;/P&gt;
&lt;P&gt;1 2 01 5&lt;/P&gt;
&lt;P&gt;1 3 01 5&lt;/P&gt;
&lt;P&gt;2 1 00 4&lt;/P&gt;
&lt;P&gt;2 2 00 5&lt;/P&gt;
&lt;P&gt;2 3 00 3&lt;/P&gt;
&lt;P&gt;2 1 01 3&lt;/P&gt;
&lt;P&gt;2 2 01 5&lt;/P&gt;
&lt;P&gt;2 3 01 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and I need:&lt;/P&gt;
&lt;P&gt;region month year mean neighbor_mean_lag&lt;/P&gt;
&lt;P&gt;1 1 00 5 .&lt;/P&gt;
&lt;P&gt;1 2 00 6 .&lt;/P&gt;
&lt;P&gt;1 3 00 4 .&lt;/P&gt;
&lt;P&gt;1 1 01 4 10&lt;/P&gt;
&lt;P&gt;1 2 01 5 13&lt;/P&gt;
&lt;P&gt;1 3 01 5 16&lt;/P&gt;
&lt;P&gt;2 1 00 10 .&lt;/P&gt;
&lt;P&gt;2 2 00 13 .&lt;/P&gt;
&lt;P&gt;2 3 00 16 .&lt;/P&gt;
&lt;P&gt;2 1 01 3 .&lt;/P&gt;
&lt;P&gt;2 2 01 5 .&lt;/P&gt;
&lt;P&gt;2 3 01 2 .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Note neighbor_mean_lag equals the neighboring region's monthly values for the previous year.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am able to create a lagged variable for the same region using the below code. However, I haven't been successful at creating the lagged variable for a neighboring region.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA lag3;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;SET lag2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; BY region month year;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; lag1_mean=lag(mean);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if first.month and first.year then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lag1_mean=.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end; &lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 16 Mar 2016 01:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-lag-by-region-month-amp-year/m-p/256937#M57192</guid>
      <dc:creator>jlajla</dc:creator>
      <dc:date>2016-03-16T01:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to lag by region, month, &amp; year?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-lag-by-region-month-amp-year/m-p/256942#M57193</link>
      <description>&lt;P&gt;A self join will be simpler than using LAG&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input region month year mean;
datalines;
1 1 00 5
1 2 00 6
1 3 00 4
1 1 01 4
1 2 01 5
1 3 01 5
2 1 00 4
2 2 00 5
2 3 00 3
2 1 01 3
2 2 01 5
2 3 01 2
;
data have1;
set have; by region notsorted;
reg + first.region;
run;

proc sql;
create table want as
select 
    a.region,
    a.month,
    a.year,
    a.mean, 
    b.mean as neighbor_mean_lag
from 
    have1 as a left join
    have1 as b 
    on  b.reg = a.reg+1 and 
        b.year = a.year-1 and 
        b.month = a.month
order by a.reg, year, month;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Mar 2016 02:31:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-lag-by-region-month-amp-year/m-p/256942#M57193</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-16T02:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to lag by region, month, &amp; year?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-lag-by-region-month-amp-year/m-p/256947#M57194</link>
      <description>&lt;P&gt;looks like it is not a mean, it is a&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;MEAN variable value for the next&amp;nbsp;region. I would try Hash Table.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Mar 2016 03:11:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-lag-by-region-month-amp-year/m-p/256947#M57194</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-03-16T03:11:28Z</dc:date>
    </item>
  </channel>
</rss>

