<?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: Assign sequential id by id and date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/467976#M119491</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 3/23/07
1 5/23/08
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a out=a1;by id;run;

data a2;
set a1;
by id;
if first.id then seq=1 ;
else seq+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Jun 2018 06:53:19 GMT</pubDate>
    <dc:creator>rajeshalwayswel</dc:creator>
    <dc:date>2018-06-06T06:53:19Z</dc:date>
    <item>
      <title>Assign sequential id by id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/467970#M119489</link>
      <description>&lt;P&gt;Hello all!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question about how to assign a sequential number to each subject based on another variable that is a date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This would be the start dataset:&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; date&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 1/10/07&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 3/23/07&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 5/23/08&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 2/10/07&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 2/01/08&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 8/01/12&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 1/23/13&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 2/10/14&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 7/10/15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I want as the end result:&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;seq&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 1/10/07 &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 3/23/07 &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 5/23/08 &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 2/10/07 &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 2/01/08 &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 8/01/12 &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 1/23/13 &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 2/10/14 &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; 7/10/15 &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advice?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 06:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/467970#M119489</guid>
      <dc:creator>SarahW13</dc:creator>
      <dc:date>2018-06-06T06:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: Assign sequential id by id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/467976#M119491</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 3/23/07
1 5/23/08
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a out=a1;by id;run;

data a2;
set a1;
by id;
if first.id then seq=1 ;
else seq+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jun 2018 06:53:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/467976#M119491</guid>
      <dc:creator>rajeshalwayswel</dc:creator>
      <dc:date>2018-06-06T06:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: Assign sequential id by id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/467978#M119493</link>
      <description>&lt;P&gt;Thank you so much! It worked!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only thing that I changed is that when I did proc sort, I did this:&lt;/P&gt;&lt;P&gt;proc sort data=dataset;&lt;/P&gt;&lt;P&gt;by id date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not sure if it would have still worked with only sorting by id...but I sorted by both id and date since I wanted the Seq to be in the order of the dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 06:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/467978#M119493</guid>
      <dc:creator>SarahW13</dc:creator>
      <dc:date>2018-06-06T06:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: Assign sequential id by id and date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/468203#M119561</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194212"&gt;@SarahW13&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you so much! It worked!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only thing that I changed is that when I did proc sort, I did this:&lt;/P&gt;
&lt;P&gt;proc sort data=dataset;&lt;/P&gt;
&lt;P&gt;by id date;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if it would have still worked with only sorting by id...but I sorted by both id and date since I wanted the Seq to be in the order of the dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You inclusion of date is correct.&lt;/P&gt;
&lt;P&gt;If you change the order of the data as read, switched the 2nd and 3rd rows and sort by Id the values are not in the desired order:&lt;/P&gt;
&lt;PRE&gt;data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 5/23/08
1 3/23/07
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a; 
by id;
run;&lt;/PRE&gt;
&lt;P&gt;But I strongly suggest using a DATE value instead of character as otherwise your data will not sort correctly with the date:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please see:&lt;/P&gt;
&lt;PRE&gt;data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 5/23/08
1 3/23/07
1 11/10/04
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a; 
by id date;
run;&lt;/PRE&gt;
&lt;P&gt;Note that 11/10/04 comes after 1/10/07. Character sort goes character by character left to right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So something more like this is likely actually what you want:&lt;/P&gt;
&lt;PRE&gt;data a;
infile datalines missover;
input id  date mmddyy8. ;
format date mmddyy8.;
datalines;
1 1/10/07
1 5/23/08
1 3/23/07
1 11/10/04
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a; 
by id date;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jun 2018 22:17:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequential-id-by-id-and-date/m-p/468203#M119561</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-06T22:17:44Z</dc:date>
    </item>
  </channel>
</rss>

