<?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 Compare row to next row based on multiple columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-row-to-next-row-based-on-multiple-columns/m-p/766976#M243083</link>
    <description>&lt;P&gt;Hello Everyone:&amp;nbsp;Can anyone please help me figure out the following code in SAS! Thank you. I'm creating columns called &lt;STRONG&gt;Migration&lt;/STRONG&gt;&amp;nbsp;and &lt;STRONG&gt;Prior Migration&lt;/STRONG&gt; using:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Migration:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;if(Acct_No=Next(Acct_No) and Cur_Flg=0 and Next(StartDate)='2021/02/28' ) then "Migration"&lt;BR /&gt;if(Acct_No=Next(Acct_No) and Cur_Flg=0 and Name='Pearl 175' and Next(Name)='Pearl 300' ) then "Migration 175 to 300"&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Prior Migration:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;if(Acct_No=Next(Acct_No) and Prior(SCD_End_Date)='2021/03/01' and ProdStartDate='2021/02/28') then "Migration"&lt;/P&gt;
&lt;P&gt;if(Acct_No=Next(Acct_No) and Name='Pearl 300' and Prior(Name)='Pearl 175' ) then "Migration 175 To 300"&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Data:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Name Acct_ No ProdStartDate ProdEndDate SCD End Date&lt;BR /&gt;Beads 25 08996 5/13/2021 5/27/2023 &lt;BR /&gt;Beads 25 10197 11/14/2019 11/27/2021 9/5/2021&lt;BR /&gt;Beads 50 11606 4/7/2020 4/21/2022 &lt;BR /&gt;Pearls 80 11739 9/26/2019 10/9/2021 3/1/2021&lt;BR /&gt;Pearls 150 11739 2/28/2021 10/9/2021 &lt;BR /&gt;Beads 50 11795 10/26/2020 11/9/2022 &lt;BR /&gt;Beads 25 11802 2/10/2021 2/24/2023 &lt;BR /&gt;Beads 25 11809 10/2/2020 10/16/2022 &lt;BR /&gt;Beads 25 13445 7/13/2021 7/27/2023 &lt;BR /&gt;Beads 25 86709 8/6/2020 8/20/2022 &lt;BR /&gt;Pearls 80 13627 11/20/2020 12/4/2022 3/1/2021&lt;BR /&gt;Pearls 150 13627 2/28/2021 12/4/2022 &lt;BR /&gt;Beads 50 13958 7/15/2021 7/29/2023 &lt;BR /&gt;Beads 25 16457 5/12/2021 5/26/2023 &lt;BR /&gt;Beads 50 88377 3/5/2020 3/19/2022 3/28/2020&lt;BR /&gt;Beads 50 88377 3/27/2020 3/19/2022 &lt;BR /&gt;Beads 25 24601 5/20/2021 6/3/2023 &lt;BR /&gt;Pearls 80 24636 11/16/2020 11/30/2022 3/1/2021&lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 5/11/2021&lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 7/27/2021&lt;BR /&gt;Beads 25 24636 7/26/2021 8/9/2023 &lt;BR /&gt;Beads 25 25706 12/5/2020 12/19/2022 &lt;BR /&gt;Beads 25 26474 9/21/2020 10/5/2022 &lt;BR /&gt;Beads 25 18721 3/4/2021 3/18/2023 &lt;BR /&gt;Pearls 175 26887 12/9/2019 12/22/2021 7/13/2020&lt;BR /&gt;Pearls 300 26887 7/12/2020 12/22/2021 &lt;BR /&gt;Beads 50 27411 6/4/2020 6/18/2022 8/5/2021&lt;BR /&gt;Pearls 80 27460 1/15/2021 1/29/2023 3/1/2021&lt;BR /&gt;Pearls 150 27460 2/28/2021 1/29/2023 &lt;BR /&gt;Beads 25 29956 2/16/2021 3/2/2023 5/21/2021&lt;BR /&gt;Beads 25 29956 5/20/2021 3/2/2023 &lt;BR /&gt;Beads 25 31100 3/26/2021 4/9/2023 &lt;BR /&gt;Beads 25 33991 12/19/2019 12/31/2021 &lt;BR /&gt;Pearls 80 35606-00001 7/9/2020 7/23/2022 3/1/2021&lt;BR /&gt;Pearls 150 35606-00001 2/28/2021 7/23/2022 &lt;BR /&gt;Beads 25 35942 4/21/2020 5/5/2022 &lt;BR /&gt;Pearls 175 45873 12/20/2019 1/2/2022 7/13/2020&lt;BR /&gt;Pearls 300 45873 7/12/2020 1/2/2022 &lt;BR /&gt;Beads 25 39923 9/17/2020 9/24/2021 12/2/2020&lt;BR /&gt;Beads 50 39923 12/1/2020 12/15/2022&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Output Needed:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Name Acct_ No ProdStartDate ProdEndDate SCD End Date Migration Prior Migration&lt;BR /&gt;Beads 25 08996 5/13/2021 5/27/2023 &lt;BR /&gt;Beads 25 10197 11/14/2019 11/27/2021 9/5/2021 &lt;BR /&gt;Beads 50 11606 4/7/2020 4/21/2022 &lt;BR /&gt;Pearls 80 11739 9/26/2019 10/9/2021 3/1/2021 Migration &lt;BR /&gt;Pearls 150 11739 2/28/2021 10/9/2021 Migration&lt;BR /&gt;Beads 50 11795 10/26/2020 11/9/2022 &lt;BR /&gt;Beads 25 11802 2/10/2021 2/24/2023 &lt;BR /&gt;Beads 25 11809 10/2/2020 10/16/2022 &lt;BR /&gt;Beads 25 13445 7/13/2021 7/27/2023 &lt;BR /&gt;Beads 25 86709 8/6/2020 8/20/2022 &lt;BR /&gt;Pearls 80 13627 11/20/2020 12/4/2022 3/1/2021 Migration &lt;BR /&gt;Pearls 150 13627 2/28/2021 12/4/2022 Migration&lt;BR /&gt;Beads 50 13958 7/15/2021 7/29/2023 &lt;BR /&gt;Beads 25 16457 5/12/2021 5/26/2023 &lt;BR /&gt;Beads 50 88377 3/5/2020 3/19/2022 3/28/2020 &lt;BR /&gt;Beads 50 88377 3/27/2020 3/19/2022 &lt;BR /&gt;Beads 25 24601 5/20/2021 6/3/2023 &lt;BR /&gt;Pearls 80 24636 11/16/2020 11/30/2022 3/1/2021 Migration &lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 5/11/2021 Migration Migration&lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 7/27/2021 &lt;BR /&gt;Beads 25 24636 7/26/2021 8/9/2023 &lt;BR /&gt;Beads 25 25706 12/5/2020 12/19/2022 &lt;BR /&gt;Beads 25 26474 9/21/2020 10/5/2022 &lt;BR /&gt;Beads 25 18721 3/4/2021 3/18/2023 &lt;BR /&gt;Pearls 175 26887 12/9/2019 12/22/2021 7/13/2020 Migration 175 To 300&lt;BR /&gt;Pearls 300 26887 7/12/2020 12/22/2021 Migration 175 To 300&lt;BR /&gt;Beads 50 27411 6/4/2020 6/18/2022 8/5/2021 &lt;BR /&gt;Pearls 80 27460 1/15/2021 1/29/2023 3/1/2021 Migration &lt;BR /&gt;Pearls 150 27460 2/28/2021 1/29/2023 Migration&lt;BR /&gt;Beads 25 29956 2/16/2021 3/2/2023 5/21/2021 &lt;BR /&gt;Beads 25 29956 5/20/2021 3/2/2023 &lt;BR /&gt;Beads 25 31100 3/26/2021 4/9/2023 &lt;BR /&gt;Beads 25 33991 12/19/2019 12/31/2021 &lt;BR /&gt;Pearls 80 35606-00001 7/9/2020 7/23/2022 3/1/2021 Migration &lt;BR /&gt;Pearls 150 35606-00001 2/28/2021 7/23/2022 Migration&lt;BR /&gt;Beads 25 35942 4/21/2020 5/5/2022 &lt;BR /&gt;Pearls 175 45873 12/20/2019 1/2/2022 7/13/2020 Migration 175 To 300&lt;BR /&gt;Pearls 300 45873 7/12/2020 1/2/2022 Migration 175 To 300&lt;BR /&gt;Beads 25 39923 9/17/2020 9/24/2021 12/2/2020 &lt;BR /&gt;Beads 50 39923 12/1/2020 12/15/2022&lt;/P&gt;</description>
    <pubDate>Thu, 09 Sep 2021 20:44:59 GMT</pubDate>
    <dc:creator>sasuser_sk</dc:creator>
    <dc:date>2021-09-09T20:44:59Z</dc:date>
    <item>
      <title>Compare row to next row based on multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-row-to-next-row-based-on-multiple-columns/m-p/766976#M243083</link>
      <description>&lt;P&gt;Hello Everyone:&amp;nbsp;Can anyone please help me figure out the following code in SAS! Thank you. I'm creating columns called &lt;STRONG&gt;Migration&lt;/STRONG&gt;&amp;nbsp;and &lt;STRONG&gt;Prior Migration&lt;/STRONG&gt; using:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Migration:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;if(Acct_No=Next(Acct_No) and Cur_Flg=0 and Next(StartDate)='2021/02/28' ) then "Migration"&lt;BR /&gt;if(Acct_No=Next(Acct_No) and Cur_Flg=0 and Name='Pearl 175' and Next(Name)='Pearl 300' ) then "Migration 175 to 300"&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Prior Migration:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;if(Acct_No=Next(Acct_No) and Prior(SCD_End_Date)='2021/03/01' and ProdStartDate='2021/02/28') then "Migration"&lt;/P&gt;
&lt;P&gt;if(Acct_No=Next(Acct_No) and Name='Pearl 300' and Prior(Name)='Pearl 175' ) then "Migration 175 To 300"&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Data:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Name Acct_ No ProdStartDate ProdEndDate SCD End Date&lt;BR /&gt;Beads 25 08996 5/13/2021 5/27/2023 &lt;BR /&gt;Beads 25 10197 11/14/2019 11/27/2021 9/5/2021&lt;BR /&gt;Beads 50 11606 4/7/2020 4/21/2022 &lt;BR /&gt;Pearls 80 11739 9/26/2019 10/9/2021 3/1/2021&lt;BR /&gt;Pearls 150 11739 2/28/2021 10/9/2021 &lt;BR /&gt;Beads 50 11795 10/26/2020 11/9/2022 &lt;BR /&gt;Beads 25 11802 2/10/2021 2/24/2023 &lt;BR /&gt;Beads 25 11809 10/2/2020 10/16/2022 &lt;BR /&gt;Beads 25 13445 7/13/2021 7/27/2023 &lt;BR /&gt;Beads 25 86709 8/6/2020 8/20/2022 &lt;BR /&gt;Pearls 80 13627 11/20/2020 12/4/2022 3/1/2021&lt;BR /&gt;Pearls 150 13627 2/28/2021 12/4/2022 &lt;BR /&gt;Beads 50 13958 7/15/2021 7/29/2023 &lt;BR /&gt;Beads 25 16457 5/12/2021 5/26/2023 &lt;BR /&gt;Beads 50 88377 3/5/2020 3/19/2022 3/28/2020&lt;BR /&gt;Beads 50 88377 3/27/2020 3/19/2022 &lt;BR /&gt;Beads 25 24601 5/20/2021 6/3/2023 &lt;BR /&gt;Pearls 80 24636 11/16/2020 11/30/2022 3/1/2021&lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 5/11/2021&lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 7/27/2021&lt;BR /&gt;Beads 25 24636 7/26/2021 8/9/2023 &lt;BR /&gt;Beads 25 25706 12/5/2020 12/19/2022 &lt;BR /&gt;Beads 25 26474 9/21/2020 10/5/2022 &lt;BR /&gt;Beads 25 18721 3/4/2021 3/18/2023 &lt;BR /&gt;Pearls 175 26887 12/9/2019 12/22/2021 7/13/2020&lt;BR /&gt;Pearls 300 26887 7/12/2020 12/22/2021 &lt;BR /&gt;Beads 50 27411 6/4/2020 6/18/2022 8/5/2021&lt;BR /&gt;Pearls 80 27460 1/15/2021 1/29/2023 3/1/2021&lt;BR /&gt;Pearls 150 27460 2/28/2021 1/29/2023 &lt;BR /&gt;Beads 25 29956 2/16/2021 3/2/2023 5/21/2021&lt;BR /&gt;Beads 25 29956 5/20/2021 3/2/2023 &lt;BR /&gt;Beads 25 31100 3/26/2021 4/9/2023 &lt;BR /&gt;Beads 25 33991 12/19/2019 12/31/2021 &lt;BR /&gt;Pearls 80 35606-00001 7/9/2020 7/23/2022 3/1/2021&lt;BR /&gt;Pearls 150 35606-00001 2/28/2021 7/23/2022 &lt;BR /&gt;Beads 25 35942 4/21/2020 5/5/2022 &lt;BR /&gt;Pearls 175 45873 12/20/2019 1/2/2022 7/13/2020&lt;BR /&gt;Pearls 300 45873 7/12/2020 1/2/2022 &lt;BR /&gt;Beads 25 39923 9/17/2020 9/24/2021 12/2/2020&lt;BR /&gt;Beads 50 39923 12/1/2020 12/15/2022&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Output Needed:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Name Acct_ No ProdStartDate ProdEndDate SCD End Date Migration Prior Migration&lt;BR /&gt;Beads 25 08996 5/13/2021 5/27/2023 &lt;BR /&gt;Beads 25 10197 11/14/2019 11/27/2021 9/5/2021 &lt;BR /&gt;Beads 50 11606 4/7/2020 4/21/2022 &lt;BR /&gt;Pearls 80 11739 9/26/2019 10/9/2021 3/1/2021 Migration &lt;BR /&gt;Pearls 150 11739 2/28/2021 10/9/2021 Migration&lt;BR /&gt;Beads 50 11795 10/26/2020 11/9/2022 &lt;BR /&gt;Beads 25 11802 2/10/2021 2/24/2023 &lt;BR /&gt;Beads 25 11809 10/2/2020 10/16/2022 &lt;BR /&gt;Beads 25 13445 7/13/2021 7/27/2023 &lt;BR /&gt;Beads 25 86709 8/6/2020 8/20/2022 &lt;BR /&gt;Pearls 80 13627 11/20/2020 12/4/2022 3/1/2021 Migration &lt;BR /&gt;Pearls 150 13627 2/28/2021 12/4/2022 Migration&lt;BR /&gt;Beads 50 13958 7/15/2021 7/29/2023 &lt;BR /&gt;Beads 25 16457 5/12/2021 5/26/2023 &lt;BR /&gt;Beads 50 88377 3/5/2020 3/19/2022 3/28/2020 &lt;BR /&gt;Beads 50 88377 3/27/2020 3/19/2022 &lt;BR /&gt;Beads 25 24601 5/20/2021 6/3/2023 &lt;BR /&gt;Pearls 80 24636 11/16/2020 11/30/2022 3/1/2021 Migration &lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 5/11/2021 Migration Migration&lt;BR /&gt;Pearls 150 24636 2/28/2021 11/30/2022 7/27/2021 &lt;BR /&gt;Beads 25 24636 7/26/2021 8/9/2023 &lt;BR /&gt;Beads 25 25706 12/5/2020 12/19/2022 &lt;BR /&gt;Beads 25 26474 9/21/2020 10/5/2022 &lt;BR /&gt;Beads 25 18721 3/4/2021 3/18/2023 &lt;BR /&gt;Pearls 175 26887 12/9/2019 12/22/2021 7/13/2020 Migration 175 To 300&lt;BR /&gt;Pearls 300 26887 7/12/2020 12/22/2021 Migration 175 To 300&lt;BR /&gt;Beads 50 27411 6/4/2020 6/18/2022 8/5/2021 &lt;BR /&gt;Pearls 80 27460 1/15/2021 1/29/2023 3/1/2021 Migration &lt;BR /&gt;Pearls 150 27460 2/28/2021 1/29/2023 Migration&lt;BR /&gt;Beads 25 29956 2/16/2021 3/2/2023 5/21/2021 &lt;BR /&gt;Beads 25 29956 5/20/2021 3/2/2023 &lt;BR /&gt;Beads 25 31100 3/26/2021 4/9/2023 &lt;BR /&gt;Beads 25 33991 12/19/2019 12/31/2021 &lt;BR /&gt;Pearls 80 35606-00001 7/9/2020 7/23/2022 3/1/2021 Migration &lt;BR /&gt;Pearls 150 35606-00001 2/28/2021 7/23/2022 Migration&lt;BR /&gt;Beads 25 35942 4/21/2020 5/5/2022 &lt;BR /&gt;Pearls 175 45873 12/20/2019 1/2/2022 7/13/2020 Migration 175 To 300&lt;BR /&gt;Pearls 300 45873 7/12/2020 1/2/2022 Migration 175 To 300&lt;BR /&gt;Beads 25 39923 9/17/2020 9/24/2021 12/2/2020 &lt;BR /&gt;Beads 50 39923 12/1/2020 12/15/2022&lt;/P&gt;</description>
      <pubDate>Thu, 09 Sep 2021 20:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-row-to-next-row-based-on-multiple-columns/m-p/766976#M243083</guid>
      <dc:creator>sasuser_sk</dc:creator>
      <dc:date>2021-09-09T20:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: Compare row to next row based on multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-row-to-next-row-based-on-multiple-columns/m-p/766985#M243087</link>
      <description>&lt;P&gt;There is no function NEXT in sas but if you can sort the file to reverse order then you can use lag function to compare to the previous observation value of a variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there is no a date variable to sort by descending, add new variable: &lt;STRONG&gt;sequence = _N_&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and sort by descending sequence.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Sep 2021 22:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-row-to-next-row-based-on-multiple-columns/m-p/766985#M243087</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-09-09T22:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: Compare row to next row based on multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-row-to-next-row-based-on-multiple-columns/m-p/766998#M243096</link>
      <description>&lt;P&gt;Do you actually have date values in those variables?&amp;nbsp; If so then to specify a constant value you need to use a date constant not a string.&amp;nbsp; Date constants have the letter d after the closing quote and the value inside the quotes has to be something the DATE informat can understand.&amp;nbsp; So '28FEB2021'd not '2021/02/28' nor '2/28/2021'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no "next" or "lead" function in a data step.&amp;nbsp; There is a LAG() function so if you could sort the data in descending order it might work better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really did want to implement a "lead" function here is a trick.&amp;nbsp; Add an extra SET statement that re-reads the same input dataset but starts with the second observation.&amp;nbsp; Use KEEP= to include the variables you want to "lead" and use RENAME= to give them new names.&amp;nbsp; Also include one more empty observation so the number of observations are the same in the two SET statements.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data is grouped by (say by NAME or ACCT_NO or both) then use BY group processing to know when to ignore the values.&lt;/P&gt;
&lt;P&gt;So something like this:&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 acct_no;
  set have(firstobs=2 keep=startdate rename=(startdate=nextdate)) have(obs=1 drop=_all_);
  prior_scd_end_date=lag(SCD_End_Date);
  if first.acct_no then call missing(prior_scd_end_date);
  if last.acct_no then call missing(nextdate);
  .... code referencing PRIOR_SCD_END_DATE and NEXTDATE .... 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Sep 2021 04:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-row-to-next-row-based-on-multiple-columns/m-p/766998#M243096</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-10T04:24:03Z</dc:date>
    </item>
  </channel>
</rss>

