<?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: Ho to add a serial number based on the previous observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Ho-to-add-a-serial-number-based-on-the-previous-observations/m-p/908673#M358559</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for supplying the data in the form of data steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added a couple of extra rows to your have data set for testing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* added 2 extra rows at end to test consecutive dates */
/* that spread over different id values are handled OK */
data have;
input id date :ddmmyy10.;
format date ddmmyy10.;
datalines;
1 20/11/2019
1 21/11/2019
1 22/11/2019
1 25/11/2019
1 27/11/2019
1 29/11/2019
2 20/11/2019
2 21/11/2019
2 23/11/2019
2 25/11/2019
2 26/11/2019
2 28/11/2019
3 29/11/2019
3 01/12/2019
3 02/12/2019
4 03/12/2019
;

/* use DOW loop to process by id */
data want2(drop = last_date);
  do until (last.id);
    set have;
    
    by id;
    
    if sum(date, -last_date) = 1 then
      column_needed + 1;
    else
      column_needed = 1;
      
    last_date = date;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The above gives the following output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Amir_0-1702924954005.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91661i979C4FEFB6625308/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Amir_0-1702924954005.png" alt="Amir_0-1702924954005.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Dec 2023 18:43:44 GMT</pubDate>
    <dc:creator>Amir</dc:creator>
    <dc:date>2023-12-18T18:43:44Z</dc:date>
    <item>
      <title>Ho to add a serial number based on the previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ho-to-add-a-serial-number-based-on-the-previous-observations/m-p/908665#M358557</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wondered if you could help me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the below dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date :ddmmyy10.;
format date ddmmyy10.;
datalines;
1 20/11/2019
1 21/11/2019
1 22/11/2019
1 25/11/2019
1 27/11/2019
1 29/11/2019
2 20/11/2019
2 21/11/2019
2 23/11/2019
2 25/11/2019
2 26/11/2019
2 28/11/2019
3 29/11/2019
3 01/12/2019
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to add a column that represents the number of dates in a row.&lt;BR /&gt;&lt;BR /&gt;This is what I wanted:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id date :ddmmyy10. COLUMN_NEEDED;
format date ddmmyy10.;
datalines;
1 20/11/2019 1
1 21/11/2019 2
1 22/11/2019 3
1 25/11/2019 1
1 27/11/2019 1
1 29/11/2019 1
2 20/11/2019 1
2 21/11/2019 2
2 23/11/2019 1
2 25/11/2019 1
2 26/11/2019 2
2 28/11/2019 1
3 29/11/2019 1
3 01/12/2019 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As long as there are dates in a row by each ID, the COLUMN_NEEDED adds 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 17:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ho-to-add-a-serial-number-based-on-the-previous-observations/m-p/908665#M358557</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2023-12-18T17:54:52Z</dc:date>
    </item>
    <item>
      <title>Re: Ho to add a serial number based on the previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ho-to-add-a-serial-number-based-on-the-previous-observations/m-p/908669#M358558</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=a);
 set have;
 retain column_needed;
 by id;
 a=lag(date);
 if first.id then column_needed=1;
 else do;
  if INTCK('day',a,date)=1 then column_needed=column_needed+1;
  else column_needed=1;
      end;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 18:10:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ho-to-add-a-serial-number-based-on-the-previous-observations/m-p/908669#M358558</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-12-18T18:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: Ho to add a serial number based on the previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ho-to-add-a-serial-number-based-on-the-previous-observations/m-p/908673#M358559</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for supplying the data in the form of data steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added a couple of extra rows to your have data set for testing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* added 2 extra rows at end to test consecutive dates */
/* that spread over different id values are handled OK */
data have;
input id date :ddmmyy10.;
format date ddmmyy10.;
datalines;
1 20/11/2019
1 21/11/2019
1 22/11/2019
1 25/11/2019
1 27/11/2019
1 29/11/2019
2 20/11/2019
2 21/11/2019
2 23/11/2019
2 25/11/2019
2 26/11/2019
2 28/11/2019
3 29/11/2019
3 01/12/2019
3 02/12/2019
4 03/12/2019
;

/* use DOW loop to process by id */
data want2(drop = last_date);
  do until (last.id);
    set have;
    
    by id;
    
    if sum(date, -last_date) = 1 then
      column_needed + 1;
    else
      column_needed = 1;
      
    last_date = date;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The above gives the following output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Amir_0-1702924954005.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91661i979C4FEFB6625308/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Amir_0-1702924954005.png" alt="Amir_0-1702924954005.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 18:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ho-to-add-a-serial-number-based-on-the-previous-observations/m-p/908673#M358559</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2023-12-18T18:43:44Z</dc:date>
    </item>
  </channel>
</rss>

