<?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: last row with pattern move from 0 to 1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949834#M371511</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by custid;
retain changemonth;
if first.custid then changemonth = .;
if not first.custid and lag(status) = 0 and status = 1 then changemonth = month;
if last.custid;
keep custid changemonth;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Add a FORMAT statement if your month variable is a SAS date value.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Nov 2024 12:44:19 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2024-11-04T12:44:19Z</dc:date>
    <item>
      <title>last row with pattern move from 0 to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949831#M371510</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a data set with multiple rows for each customerID.&lt;/P&gt;
&lt;P&gt;Each row represent data on other month.&lt;/P&gt;
&lt;P&gt;I want to find the last month (The closest month to current month) that Customer moved from&amp;nbsp; status 0 to status 1.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;For customer 123456&lt;/P&gt;
&lt;P&gt;2024-01&amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-02&amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;2024-03&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;2024-04&amp;nbsp; 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2024-05&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-06&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-07&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-08 1&lt;/P&gt;
&lt;P&gt;2024-09 0&lt;/P&gt;
&lt;P&gt;2024-10 0&lt;/P&gt;
&lt;P&gt;2024-11 0&lt;/P&gt;
&lt;P&gt;You can see that last month of change from status 0 to 1 is 2024-08&lt;/P&gt;
&lt;P&gt;What is the way to find it by code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID status
123 202401 0
123 202402 0
123 202403 1
123 202404 1
123 202405 0
123 202406 0
123 202407 1
123 202408 0
123 202409 0
123 202410 0
123 20411 0
;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2024 12:27:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949831#M371510</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-11-04T12:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: last row with pattern move from 0 to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949834#M371511</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by custid;
retain changemonth;
if first.custid then changemonth = .;
if not first.custid and lag(status) = 0 and status = 1 then changemonth = month;
if last.custid;
keep custid changemonth;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Add a FORMAT statement if your month variable is a SAS date value.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2024 12:44:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949834#M371511</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-11-04T12:44:19Z</dc:date>
    </item>
    <item>
      <title>Re: last row with pattern move from 0 to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949835#M371512</link>
      <description>&lt;P&gt;Please proofread your code, and then run it yourself to make sure it works. Do &lt;STRONG&gt;NOT&lt;/STRONG&gt; provide us sample data with code that doesn't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use LAG to determine previous month status in a data step and create flag to indicate when the change happens, then use PROC SUMMARY to find the max month where this happens. I can provide examples once I have working code that creates the sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2024 12:55:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949835#M371512</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-11-04T12:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: last row with pattern move from 0 to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949841#M371513</link>
      <description>&lt;P&gt;What would you like for this abbreviated version of your data?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID status;
datalines;
123 202401 0
123 202402 0
123 202403 1
123 202404 1
123 202405 0
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Nov 2024 14:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949841#M371513</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-11-04T14:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: last row with pattern move from 0 to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949857#M371517</link>
      <description>&lt;P&gt;Please look at this and FIX WHICH DATE HAS STATUS 1 and WHICH STATUS 0.&lt;/P&gt;
&lt;P&gt;Bad enough that that the data step 1) doesn't run 2) would never have status as 0 or 1 as you are reading the date as the Status variable.&lt;/P&gt;
&lt;P&gt;But then&amp;nbsp; you complicate things by changing the values of status for 2024-07 and 2024_08 so we aren't even sure which is correct for discussion.&lt;/P&gt;
&lt;P&gt;Plus the Last "date" is incorrect as 20411 not 202411 &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Really should show what the output is supposed to look like and include expected output for cases where status is all 0 or all 1 for a custid or never changes from 0 to 1 for other reasons (only 0 is the status for the last sequential month for the customer )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;For customer 123456&lt;/P&gt;
&lt;P&gt;2024-01&amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-02&amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;2024-03&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;2024-04&amp;nbsp; 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2024-05&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-06&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-07&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;2024-08 1&lt;/P&gt;
&lt;P&gt;2024-09 0&lt;/P&gt;
&lt;P&gt;2024-10 0&lt;/P&gt;
&lt;P&gt;2024-11 0&lt;/P&gt;
&lt;P&gt;You can see that last month of change from status 0 to 1 is 2024-08&lt;/P&gt;
&lt;P&gt;What is the way to find it by code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;Data have;
input CustID status
123 202401 0
123 202402 0
123 202403 1
123 202404 1
123 202405 0
123 202406 0
123 202407 1
123 202408 0
123 202409 0
123 202410 0
123 20411 0
;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 15:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/949857#M371517</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-11-06T15:56:19Z</dc:date>
    </item>
    <item>
      <title>Re: last row with pattern move from 0 to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/950041#M371556</link>
      <description>&lt;P&gt;Let's move the goal posts a little, and change the objective to keep the entire record (i.e. with possible hundreds of variables) that belongs to the last obs status=1 and immediately follows a status=0&amp;nbsp; (within the same custid of course).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then this two-step (but only one pass of stored data) approach is neat and efficient:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need / view=need;
  set have;
  by custid;
  if first.id=0 and lag(status)=0 and status=1;
run;

data want;
  set need;
  by custid;
  if last.custid;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 15:50:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-row-with-pattern-move-from-0-to-1/m-p/950041#M371556</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-11-06T15:50:53Z</dc:date>
    </item>
  </channel>
</rss>

