<?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: Creating a binary flag based on the next row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704525#M215991</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; solution works, But it just flags the last record for each purchase, except on the last record for the person. &lt;BR /&gt;&lt;BR /&gt;BY groups processing makes this fairly trivial and likely that type of logic is useful for your next steps.</description>
    <pubDate>Tue, 08 Dec 2020 19:52:38 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-12-08T19:52:38Z</dc:date>
    <item>
      <title>Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704464#M215957</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a dataset that looks like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have&lt;/P&gt;&lt;P&gt;Customer Total_Purchases&lt;/P&gt;&lt;P&gt;1 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 3&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want&lt;/P&gt;&lt;P&gt;Customer Total_Purchases New_Purchase&lt;/P&gt;&lt;P&gt;1 1 0&lt;/P&gt;&lt;P&gt;1 1 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1 1&lt;/P&gt;&lt;P&gt;1 2 0&lt;/P&gt;&lt;P&gt;1 2 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 3 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially what I want to do is create a flag (New_Purchase) that indicates 1 if and only if the&amp;nbsp;&lt;EM&gt;next&lt;/EM&gt; row increments up by 1 and 0 otherwise. So in this case the New_Purchase variable will equal 1 as highlighted in the want dataset as an example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2020 17:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704464#M215957</guid>
      <dc:creator>CEdward</dc:creator>
      <dc:date>2020-12-08T17:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704467#M215958</link>
      <description>&lt;P&gt;You could use a "look-ahead". However, I note you haven't really told us what happens if the data switches to a different customer, and so you should really keep in mind that you need to provide us realistic examples.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I was going to produce code for a "look-ahead", but it would only work on your data which is for one customer, and so it would be relatively useless code. I think I'll wait until you address the above issue.&lt;/P&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2020 17:36:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704467#M215958</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-08T17:36:10Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704469#M215961</link>
      <description>Paige,&lt;BR /&gt;The same pattern would repeat for all customers in the data.</description>
      <pubDate>Tue, 08 Dec 2020 17:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704469#M215961</guid>
      <dc:creator>CEdward</dc:creator>
      <dc:date>2020-12-08T17:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704475#M215965</link>
      <description>&lt;P&gt;I'm afraid I don't understand what you want to do when you get a new customer. If total_purchases on the first row of customer 2 increment by 1 from the last row of customer 1, what do you want to do there?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2020 17:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704475#M215965</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-08T17:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704479#M215967</link>
      <description>&lt;P&gt;What happens in this case?&lt;/P&gt;
&lt;P&gt;If 8 still gets a one the solution below should work.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Have
Customer Total_Purchases
1 1 
1 1 
1 1 
1 2 
1 2 
1 8 

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For your example data this works but won't scale and is likely wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by customer total_purchases;
if last.total_purchases then flag=1;
else flag=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>Tue, 08 Dec 2020 17:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704479#M215967</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-12-08T17:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704484#M215969</link>
      <description>I see.&lt;BR /&gt;So every customer starts at a single purchase. The data would never look like this:&lt;BR /&gt;Have&lt;BR /&gt;&lt;BR /&gt;Customer Total_Purchases&lt;BR /&gt;1 1&lt;BR /&gt;1 1&lt;BR /&gt;1 1&lt;BR /&gt;1 2&lt;BR /&gt;1 2&lt;BR /&gt;1 3&lt;BR /&gt;2 4&lt;BR /&gt;2 4&lt;BR /&gt;2 4&lt;BR /&gt;2 5&lt;BR /&gt;&lt;BR /&gt;Is this what you are asking? In this example, you are right, the last row of customer 1 would get a 1 in the New_Purchase flag. This isn't preferred.</description>
      <pubDate>Tue, 08 Dec 2020 18:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704484#M215969</guid>
      <dc:creator>CEdward</dc:creator>
      <dc:date>2020-12-08T18:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704488#M215971</link>
      <description>&lt;P&gt;Okay, so now we know what the data for two customers will not look like. Can you show us a realistic example of data for two customers and which flags you want where?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2020 18:10:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704488#M215971</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-08T18:10:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704519#M215988</link>
      <description>&lt;P&gt;Here's how I see it.&amp;nbsp; Assuming your data is in the proper order as you have presented it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by customer total_purchases;
   new_purchase = last.total_purchases and not last.customer;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2020 19:15:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704519#M215988</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-12-08T19:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704521#M215989</link>
      <description>Hope this clears it up:&lt;BR /&gt;&lt;BR /&gt;Have&lt;BR /&gt;&lt;BR /&gt;Customer Total_Purchases&lt;BR /&gt;1 1&lt;BR /&gt;1 1&lt;BR /&gt;1 1&lt;BR /&gt;1 2&lt;BR /&gt;1 2&lt;BR /&gt;1 3&lt;BR /&gt;2 1&lt;BR /&gt;2 1&lt;BR /&gt;2 1&lt;BR /&gt;2 2&lt;BR /&gt;2 2&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Customer Total_Purchases New_Purchase&lt;BR /&gt;1 1 0&lt;BR /&gt;1 1 0&lt;BR /&gt;1 1 1&lt;BR /&gt;1 2 0&lt;BR /&gt;1 2 1&lt;BR /&gt;1 3 0&lt;BR /&gt;2 1 0&lt;BR /&gt;2 1 0&lt;BR /&gt;2 1 1&lt;BR /&gt;2 2 0&lt;BR /&gt;2 2 0&lt;BR /&gt;&lt;BR /&gt;Does that help?</description>
      <pubDate>Tue, 08 Dec 2020 19:25:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704521#M215989</guid>
      <dc:creator>CEdward</dc:creator>
      <dc:date>2020-12-08T19:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704525#M215991</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; solution works, But it just flags the last record for each purchase, except on the last record for the person. &lt;BR /&gt;&lt;BR /&gt;BY groups processing makes this fairly trivial and likely that type of logic is useful for your next steps.</description>
      <pubDate>Tue, 08 Dec 2020 19:52:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704525#M215991</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-12-08T19:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a binary flag based on the next row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704710#M216067</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer Total_Purchases ;
cards;
1 1
1 1
1 1
1 2
1 2
1 3
2 1
2 1
2 1
2 2
2 2
;
data want;
 merge have have(keep=Customer Total_Purchases
          rename=(Customer=_Customer Total_Purchases=_Total_Purchases) firstobs=2);
 if  Customer=_Customer  and  Total_Purchases+1=_Total_Purchases  then  New_Purchase=1;
  else  New_Purchase=0;
drop _: ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Dec 2020 13:47:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-binary-flag-based-on-the-next-row/m-p/704710#M216067</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-12-09T13:47:20Z</dc:date>
    </item>
  </channel>
</rss>

