<?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: Generate a sequence number (a variable)  by ID with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911312#M359366</link>
    <description>&lt;P&gt;Just use a retained variable to count the number of cases where differences is larger than 10.&lt;/P&gt;
&lt;P&gt;To get that strange result where the count is MISSING on some observations you will need a second variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ DATE :yymmdd. DIFFERENCE ;
  format date yymmdd10. ;
datalines;
A 2019-04-22 .
A 2019-04-24 2
A 2019-04-25 1
A 2019-05-06 11
A 2019-05-17 11
A 2019-05-22 5
B 2020-02-04 .
B 2020-02-12 8
B 2020-03-04 21
B 2020-03-10 6
;

data want;
  set have ;
  by id date ;
  if first.id then count=0;
  if difference &amp;gt; 10 then do;
    count+1;
    list=count;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    ID          DATE    DIFFERENCE    count    list

  1    A     2019-04-22         .          0        .
  2    A     2019-04-24         2          0        .
  3    A     2019-04-25         1          0        .
  4    A     2019-05-06        11          1        1
  5    A     2019-05-17        11          2        2
  6    A     2019-05-22         5          2        .
  7    B     2020-02-04         .          0        .
  8    B     2020-02-12         8          0        .
  9    B     2020-03-04        21          1        1
 10    B     2020-03-10         6          1        .

&lt;/PRE&gt;
&lt;P&gt;If you don't already have the DIFERENCES variable you can calculate it on the fly using the DIF() function.&amp;nbsp; Just remember to replace the result when you start a new BY group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ DATE :yymmdd. ;;
  format date yymmdd10. ;
datalines;
A 2019-04-22
A 2019-04-24
A 2019-04-25
A 2019-05-06
A 2019-05-17
A 2019-05-22
B 2020-02-04
B 2020-02-12
B 2020-03-04
B 2020-03-10
;

data want;
  set have ;
  by id date ;
  difference = dif(date);
  if first.id then difference=.;
  if first.id then count=0;
  if difference &amp;gt; 10 then do; 
    count+1;
    list=count;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Displaying dates without the century will cause confusion.&amp;nbsp; Displaying dates in ether MDY or DMY order will confuse half of your audience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jan 2024 18:53:40 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-01-11T18:53:40Z</dc:date>
    <item>
      <title>Generate a sequence number (a variable)  by ID with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911308#M359362</link>
      <description>&lt;P&gt;I need a quick help.&lt;/P&gt;
&lt;P&gt;Wish to generate a variable as sequence number by ID but with a condition (variable named difference is greater than 10 and by id and sequence of the date)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Thank you so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID$ DATE$ DIFFERENCE$;&lt;BR /&gt;datalines;&lt;BR /&gt;A 4/22/19 .&lt;BR /&gt;A 4/24/19 2&lt;BR /&gt;A 4/25/19 1&lt;BR /&gt;A 5/6/19 11&lt;BR /&gt;A 5/17/19 11&lt;BR /&gt;A 5/22/19 5&lt;BR /&gt;B 2/4/20 .&lt;BR /&gt;B 2/12/20 8&lt;BR /&gt;B 3/4/20 21&lt;BR /&gt;B 3/10/20 6&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data Wish something like below (i.e. create a variable named list which is a sequence number by ID but with the condition that the variable difference greater than 10 and by id's date order)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="290"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;ID&lt;/TD&gt;
&lt;TD width="91"&gt;DATE&lt;/TD&gt;
&lt;TD width="64"&gt;difference&lt;/TD&gt;
&lt;TD width="64"&gt;list&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;A&lt;/TD&gt;
&lt;TD width="91"&gt;4/22/19&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;A&lt;/TD&gt;
&lt;TD width="91"&gt;4/24/19&lt;/TD&gt;
&lt;TD width="64"&gt;2&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;A&lt;/TD&gt;
&lt;TD width="91"&gt;4/25/19&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;A&lt;/TD&gt;
&lt;TD width="91"&gt;5/6/19&lt;/TD&gt;
&lt;TD width="64"&gt;11&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;A&lt;/TD&gt;
&lt;TD width="91"&gt;5/17/19&lt;/TD&gt;
&lt;TD width="64"&gt;11&lt;/TD&gt;
&lt;TD width="64"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;A&lt;/TD&gt;
&lt;TD width="91"&gt;5/22/19&lt;/TD&gt;
&lt;TD width="64"&gt;5&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;B&lt;/TD&gt;
&lt;TD width="91"&gt;2/4/20&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;B&lt;/TD&gt;
&lt;TD width="91"&gt;2/12/20&lt;/TD&gt;
&lt;TD width="64"&gt;8&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;B&lt;/TD&gt;
&lt;TD width="91"&gt;3/4/20&lt;/TD&gt;
&lt;TD width="64"&gt;21&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="71"&gt;B&lt;/TD&gt;
&lt;TD width="91"&gt;3/10/20&lt;/TD&gt;
&lt;TD width="64"&gt;6&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 11 Jan 2024 18:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911308#M359362</guid>
      <dc:creator>chimei0403</dc:creator>
      <dc:date>2024-01-11T18:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a sequence number (a variable)  by ID with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911309#M359363</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by ID;
if first.ID then L=1; drop L;

if DIFFERENCE &amp;gt;=10 then
  do;
    list = L;
    L+1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jan 2024 18:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911309#M359363</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-01-11T18:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a sequence number (a variable)  by ID with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911310#M359364</link>
      <description>&lt;P&gt;BTW. in your data step it should be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input ID$ DATE$ DIFFERENCE;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;since the "difference" is a number.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 18:48:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911310#M359364</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-01-11T18:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a sequence number (a variable)  by ID with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911311#M359365</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you so much for the solution/advice&amp;nbsp; promptly. I truly appreciate the help!!!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 18:52:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911311#M359365</guid>
      <dc:creator>chimei0403</dc:creator>
      <dc:date>2024-01-11T18:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a sequence number (a variable)  by ID with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911312#M359366</link>
      <description>&lt;P&gt;Just use a retained variable to count the number of cases where differences is larger than 10.&lt;/P&gt;
&lt;P&gt;To get that strange result where the count is MISSING on some observations you will need a second variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ DATE :yymmdd. DIFFERENCE ;
  format date yymmdd10. ;
datalines;
A 2019-04-22 .
A 2019-04-24 2
A 2019-04-25 1
A 2019-05-06 11
A 2019-05-17 11
A 2019-05-22 5
B 2020-02-04 .
B 2020-02-12 8
B 2020-03-04 21
B 2020-03-10 6
;

data want;
  set have ;
  by id date ;
  if first.id then count=0;
  if difference &amp;gt; 10 then do;
    count+1;
    list=count;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    ID          DATE    DIFFERENCE    count    list

  1    A     2019-04-22         .          0        .
  2    A     2019-04-24         2          0        .
  3    A     2019-04-25         1          0        .
  4    A     2019-05-06        11          1        1
  5    A     2019-05-17        11          2        2
  6    A     2019-05-22         5          2        .
  7    B     2020-02-04         .          0        .
  8    B     2020-02-12         8          0        .
  9    B     2020-03-04        21          1        1
 10    B     2020-03-10         6          1        .

&lt;/PRE&gt;
&lt;P&gt;If you don't already have the DIFERENCES variable you can calculate it on the fly using the DIF() function.&amp;nbsp; Just remember to replace the result when you start a new BY group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ DATE :yymmdd. ;;
  format date yymmdd10. ;
datalines;
A 2019-04-22
A 2019-04-24
A 2019-04-25
A 2019-05-06
A 2019-05-17
A 2019-05-22
B 2020-02-04
B 2020-02-12
B 2020-03-04
B 2020-03-10
;

data want;
  set have ;
  by id date ;
  difference = dif(date);
  if first.id then difference=.;
  if first.id then count=0;
  if difference &amp;gt; 10 then do; 
    count+1;
    list=count;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Displaying dates without the century will cause confusion.&amp;nbsp; Displaying dates in ether MDY or DMY order will confuse half of your audience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 18:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911312#M359366</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-11T18:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a sequence number (a variable)  by ID with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911314#M359368</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for the advice as well! I will practice a bit and see how those output go!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2024 19:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911314#M359368</guid>
      <dc:creator>chimei0403</dc:creator>
      <dc:date>2024-01-11T19:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a sequence number (a variable)  by ID with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911452#M359405</link>
      <description>&lt;P&gt;Your variable DIFFERENCE appears to be DATE-lag(DATE)&amp;nbsp; [otherwise known as DIF(date)], ... except for the first obs of each ID, when it takes a missing values.&amp;nbsp; So with access to the DIF function, you could:&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 :yymmdd. DIFFERENCE ;
  format date yymmdd10. ;
datalines;
A 2019-04-22 .
A 2019-04-24 2
A 2019-04-25 1
A 2019-05-06 11
A 2019-05-17 11
A 2019-05-22 5
B 2020-02-04 .
B 2020-02-12 8
B 2020-03-04 21
B 2020-03-10 6
;
data want (drop=_:);
  set have ;
  by id;
  retain _x;  /*Edited addition */
  _x=ifn(first.id,0,sum(_x,dif(date)&amp;gt;10));
  if dif(date)&amp;gt;10 and first.id=0 then list=_x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 23:23:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-sequence-number-a-variable-by-ID-with-condition/m-p/911452#M359405</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-01-12T23:23:23Z</dc:date>
    </item>
  </channel>
</rss>

