<?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: Lag based on Month in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Lag-based-on-Month/m-p/411433#M66096</link>
    <description>&lt;P&gt;What Lag function does is to shift data by row. There are not other definitions of this function.&lt;/P&gt;
&lt;P&gt;I might misunderstand the objective. Do you mean lagging variables (not M/Y) by a month, not by row? An issue which row-shifting based Lag is not desired is that you have multiple ID in the data, and you don't want to Customer A's data spill off to Customer B's rows. A JSL trick can address it. Another situation which by-row shift is not desired is that there are missing observations. For an example, you have monthly series for a year, but missing June. The row for June just does not exist in the data. A row-shifting based Lag will totally mess up the data. There may not have simple JSL tricks. &lt;/P&gt;
&lt;P&gt;If my understand correctly about the objective, I have a thought. First, let's simplify the data for the illustration purpose. I cut the data down to three columns: SP_ID, M/Y, WTR_GAL. The objective is to shift WTR_GAL, by SP_ID, based on time M/Y.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_0-1629422156918.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35198iA4C47A00465C1CE3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_0-1629422156918.png" alt="peng_liu_0-1629422156918.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I create a copy of this table, and create two more columns:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_1-1629422277460.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35199i46D5A5E41D625287/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_1-1629422277460.png" alt="peng_liu_1-1629422277460.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The formula for Lag(M/Y) is&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_2-1629422329386.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35200iD3B1488991590252/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_2-1629422329386.png" alt="peng_liu_2-1629422329386.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The formula for Lag WTR_GAL is the following, which is basically a copy of WTR_GAL.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_3-1629422363002.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35201i4B6FC84F8138D2C3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_3-1629422363002.png" alt="peng_liu_3-1629422363002.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Then I join the two tables together by matching SP_ID and M/Y==Lag(M/Y). Here is the JOIN dialog&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_4-1629422496109.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35202i8F6447BC7881CDEF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_4-1629422496109.png" alt="peng_liu_4-1629422496109.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;And here is the result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_5-1629422574356.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35203i722C7AFC25135325/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_5-1629422574356.png" alt="peng_liu_5-1629422574356.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Notice the highlighted cell is something that one has to pay attention if using the row-shifting based Lag. But here, without row-shifting based lag, the result is what I expect. The entire operation is piecing this month data with last month data by JOIN, the result is the same as the "ideal" LAG operation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 20 Aug 2021 01:29:28 GMT</pubDate>
    <dc:creator>peng_liu</dc:creator>
    <dc:date>2021-08-20T01:29:28Z</dc:date>
    <item>
      <title>Lag based on Month</title>
      <link>https://community.jmp.com/t5/Discussions/Lag-based-on-Month/m-p/411398#M66094</link>
      <description>&lt;P&gt;Our goal is to create a 1 month lag column (rather than by row).&amp;nbsp; The LAG(M/Y) column output of 01/2000 should line up with 02/2000 in the M/Y column, etc.&amp;nbsp; Attached is a tiny subset example of water use by m/y; whereas, our full table contains more than 59 million rows.&amp;nbsp; &amp;nbsp;The full table consists of water use for more than 430,000 customers for each each month over many years.&amp;nbsp; We're open to all suggestions, if using a different column such as CONS_MONTH_NUM is easier or more straightforward.&amp;nbsp; Could this possibly be resolved using Group By?&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 23:35:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Lag-based-on-Month/m-p/411398#M66094</guid>
      <dc:creator>Dukowitz62</dc:creator>
      <dc:date>2023-06-10T23:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Lag based on Month</title>
      <link>https://community.jmp.com/t5/Discussions/Lag-based-on-Month/m-p/411433#M66096</link>
      <description>&lt;P&gt;What Lag function does is to shift data by row. There are not other definitions of this function.&lt;/P&gt;
&lt;P&gt;I might misunderstand the objective. Do you mean lagging variables (not M/Y) by a month, not by row? An issue which row-shifting based Lag is not desired is that you have multiple ID in the data, and you don't want to Customer A's data spill off to Customer B's rows. A JSL trick can address it. Another situation which by-row shift is not desired is that there are missing observations. For an example, you have monthly series for a year, but missing June. The row for June just does not exist in the data. A row-shifting based Lag will totally mess up the data. There may not have simple JSL tricks. &lt;/P&gt;
&lt;P&gt;If my understand correctly about the objective, I have a thought. First, let's simplify the data for the illustration purpose. I cut the data down to three columns: SP_ID, M/Y, WTR_GAL. The objective is to shift WTR_GAL, by SP_ID, based on time M/Y.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_0-1629422156918.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35198iA4C47A00465C1CE3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_0-1629422156918.png" alt="peng_liu_0-1629422156918.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I create a copy of this table, and create two more columns:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_1-1629422277460.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35199i46D5A5E41D625287/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_1-1629422277460.png" alt="peng_liu_1-1629422277460.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The formula for Lag(M/Y) is&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_2-1629422329386.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35200iD3B1488991590252/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_2-1629422329386.png" alt="peng_liu_2-1629422329386.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The formula for Lag WTR_GAL is the following, which is basically a copy of WTR_GAL.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_3-1629422363002.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35201i4B6FC84F8138D2C3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_3-1629422363002.png" alt="peng_liu_3-1629422363002.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Then I join the two tables together by matching SP_ID and M/Y==Lag(M/Y). Here is the JOIN dialog&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_4-1629422496109.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35202i8F6447BC7881CDEF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_4-1629422496109.png" alt="peng_liu_4-1629422496109.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;And here is the result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="peng_liu_5-1629422574356.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/35203i722C7AFC25135325/image-size/medium?v=v2&amp;amp;px=400" role="button" title="peng_liu_5-1629422574356.png" alt="peng_liu_5-1629422574356.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Notice the highlighted cell is something that one has to pay attention if using the row-shifting based Lag. But here, without row-shifting based lag, the result is what I expect. The entire operation is piecing this month data with last month data by JOIN, the result is the same as the "ideal" LAG operation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Aug 2021 01:29:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Lag-based-on-Month/m-p/411433#M66096</guid>
      <dc:creator>peng_liu</dc:creator>
      <dc:date>2021-08-20T01:29:28Z</dc:date>
    </item>
  </channel>
</rss>

