<?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 sequence id within group and order by date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944409#M370025</link>
    <description>&lt;P&gt;Just in case you need it later below a code option that assigns per person_id only one ID per agent.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile datalines dlm=' ' dsd truncover;
  input
    person_id $
    Begin_Date :mmddyy10.
    End_Date :mmddyy10.
    Agent_ID $
    Agent_ID_Assigned
  ;
  format
    Begin_Date
    End_Date yymmdd10.
  ;
  datalines;
11111 3/2/2018 3/13/2018 A1111 1
22222 1/11/2018 1/15/2018 A1234 1
22222 1/28/2018 2/2/2018 A1234 1
22222 2/8/2018 2/10/2018 A1200 2
22222 2/24/2018 2/26/2018 A1211 3
22222 8/17/2018 8/22/2018 A4111 4
33333 3/17/2018 3/23/2018 A2111 1
44444 3/13/2018 3/15/2018 A3111 1
44444 4/24/2018 4/26/2018 A3211 2
44444 7/13/2018 7/14/2018 A3211 2
44444 7/14/2018 7/19/2018 A1212 3
44444 7/26/2018 7/30/2018 A1212 3
44444 11/17/2018 11/19/2018 A1212 3
44444 11/19/2018 11/26/2018 A1212 3
44444 3/13/2019 3/15/2018 A3111 1
;

data derived;
  if _n_=1 then
    do;
      dcl hash h1();
      h1.defineKey('agent_id');
      h1.defineData('agent_id_derived');
      h1.defineDone();
    end;
  set want;
  by person_id Begin_Date;
  length agent_id_derived 8;
  if first.person_id then h1.clear();
  if h1.find() ne 0 then 
    do;
      agent_id_derived=h1.num_items+1;
      h1.add();
    end;
run;

proc print data=derived;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 18 Sep 2024 13:15:44 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2024-09-18T13:15:44Z</dc:date>
    <item>
      <title>Assign sequence id within group and order by date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944338#M370007</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data file with each Person_ID having multiple records associated with Agent_IDs. The date information (Begin_Date and End_Date) is also available. I wanted to have the data sorted by Person_ID, dates and then Agent_ID and assign new ID (Agent_ID_Assigned in data file "want") for each Agent_ID by sorting order. Essentially Agent_ID_Assigned takes squence id ordered by date per Person_ID. The variable&amp;nbsp;Agent_ID_Assigned is the target field to generate as shown in "want" data file below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestion is greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile datalines dlm="09"x dsd truncover;&lt;BR /&gt;input&lt;BR /&gt;person_id $&lt;BR /&gt;Begin_Date :mmddyy10.&lt;BR /&gt;End_Date :mmddyy10.&lt;BR /&gt;Agent_ID $&lt;BR /&gt;;&lt;BR /&gt;format&lt;BR /&gt;Begin_Date&lt;BR /&gt;End_Date yymmdd10.&lt;BR /&gt;;&lt;BR /&gt;datalines;&lt;BR /&gt;11111 3/2/2018 3/13/2018 A1111&lt;BR /&gt;22222 1/11/2018 1/15/2018 A1234&lt;BR /&gt;22222 1/28/2018 2/2/2018 A1234&lt;BR /&gt;22222 2/8/2018 2/10/2018 A1200&lt;BR /&gt;22222 2/24/2018 2/26/2018 A1211&lt;BR /&gt;22222 8/17/2018 8/22/2018 A4111&lt;BR /&gt;33333 3/17/2018 3/23/2018 A2111&lt;BR /&gt;44444 3/13/2018 3/15/2018 A3111&lt;BR /&gt;44444 4/24/2018 4/26/2018 A3211&lt;BR /&gt;44444 7/13/2018 7/14/2018 A3211&lt;BR /&gt;44444 7/14/2018 7/19/2018 A1212&lt;BR /&gt;44444 7/26/2018 7/30/2018 A1212&lt;BR /&gt;44444 11/17/2018 11/19/2018 A1212&lt;BR /&gt;44444 11/19/2018 11/26/2018 A1212&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile datalines dlm="09"x dsd truncover;&lt;BR /&gt;input&lt;BR /&gt;person_id $&lt;BR /&gt;Begin_Date :mmddyy10.&lt;BR /&gt;End_Date :mmddyy10.&lt;BR /&gt;Agent_ID $&lt;BR /&gt;Agent_ID_Assigned&lt;BR /&gt;;&lt;BR /&gt;format&lt;BR /&gt;Begin_Date&lt;BR /&gt;End_Date yymmdd10.&lt;BR /&gt;;&lt;BR /&gt;datalines;&lt;BR /&gt;11111 3/2/2018 3/13/2018 A1111 1&lt;BR /&gt;22222 1/11/2018 1/15/2018 A1234 1&lt;BR /&gt;22222 1/28/2018 2/2/2018 A1234 1&lt;BR /&gt;22222 2/8/2018 2/10/2018 A1200 2&lt;BR /&gt;22222 2/24/2018 2/26/2018 A1211 3&lt;BR /&gt;22222 8/17/2018 8/22/2018 A4111 4&lt;BR /&gt;33333 3/17/2018 3/23/2018 A2111 1&lt;BR /&gt;44444 3/13/2018 3/15/2018 A3111 1&lt;BR /&gt;44444 4/24/2018 4/26/2018 A3211 2&lt;BR /&gt;44444 7/13/2018 7/14/2018 A3211 2&lt;BR /&gt;44444 7/14/2018 7/19/2018 A1212 3&lt;BR /&gt;44444 7/26/2018 7/30/2018 A1212 3&lt;BR /&gt;44444 11/17/2018 11/19/2018 A1212 3&lt;BR /&gt;44444 11/19/2018 11/26/2018 A1212 3&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 04:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944338#M370007</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2024-09-18T04:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Assign sequence id within group and order by date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944348#M370009</link>
      <description>&lt;P&gt;Using your sample data below should work.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile datalines dlm=' ' dsd truncover;
  input
    person_id $
    Begin_Date :mmddyy10.
    End_Date :mmddyy10.
    Agent_ID $
    Agent_ID_Assigned
  ;
  format
    Begin_Date
    End_Date yymmdd10.
  ;
  datalines;
11111 3/2/2018 3/13/2018 A1111 1
22222 1/11/2018 1/15/2018 A1234 1
22222 1/28/2018 2/2/2018 A1234 1
22222 2/8/2018 2/10/2018 A1200 2
22222 2/24/2018 2/26/2018 A1211 3
22222 8/17/2018 8/22/2018 A4111 4
33333 3/17/2018 3/23/2018 A2111 1
44444 3/13/2018 3/15/2018 A3111 1
44444 4/24/2018 4/26/2018 A3211 2
44444 7/13/2018 7/14/2018 A3211 2
44444 7/14/2018 7/19/2018 A1212 3
44444 7/26/2018 7/30/2018 A1212 3
44444 11/17/2018 11/19/2018 A1212 3
44444 11/19/2018 11/26/2018 A1212 3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data derived(drop=_:);
  set want;
  by person_id Begin_Date;
  _lag_Agent_ID=lag(Agent_ID);
  if first.person_id then agent_id_derived=1;
  else if _lag_Agent_ID ne Agent_ID then agent_id_derived+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Question:&lt;/P&gt;
&lt;P&gt;It's not a case in your sample data but in case it's possible: What should happen if for the same person a previous agent_id shows up again? like: agent A, agent B, agent A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 06:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944348#M370009</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-09-18T06:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: Assign sequence id within group and order by date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944401#M370022</link>
      <description>Thanks, Patrick! &lt;BR /&gt;It is likely that a previous agend_id shows up again for the same person. But for my current purpose, your code is sufficient.</description>
      <pubDate>Wed, 18 Sep 2024 13:00:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944401#M370022</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2024-09-18T13:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: Assign sequence id within group and order by date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944409#M370025</link>
      <description>&lt;P&gt;Just in case you need it later below a code option that assigns per person_id only one ID per agent.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile datalines dlm=' ' dsd truncover;
  input
    person_id $
    Begin_Date :mmddyy10.
    End_Date :mmddyy10.
    Agent_ID $
    Agent_ID_Assigned
  ;
  format
    Begin_Date
    End_Date yymmdd10.
  ;
  datalines;
11111 3/2/2018 3/13/2018 A1111 1
22222 1/11/2018 1/15/2018 A1234 1
22222 1/28/2018 2/2/2018 A1234 1
22222 2/8/2018 2/10/2018 A1200 2
22222 2/24/2018 2/26/2018 A1211 3
22222 8/17/2018 8/22/2018 A4111 4
33333 3/17/2018 3/23/2018 A2111 1
44444 3/13/2018 3/15/2018 A3111 1
44444 4/24/2018 4/26/2018 A3211 2
44444 7/13/2018 7/14/2018 A3211 2
44444 7/14/2018 7/19/2018 A1212 3
44444 7/26/2018 7/30/2018 A1212 3
44444 11/17/2018 11/19/2018 A1212 3
44444 11/19/2018 11/26/2018 A1212 3
44444 3/13/2019 3/15/2018 A3111 1
;

data derived;
  if _n_=1 then
    do;
      dcl hash h1();
      h1.defineKey('agent_id');
      h1.defineData('agent_id_derived');
      h1.defineDone();
    end;
  set want;
  by person_id Begin_Date;
  length agent_id_derived 8;
  if first.person_id then h1.clear();
  if h1.find() ne 0 then 
    do;
      agent_id_derived=h1.num_items+1;
      h1.add();
    end;
run;

proc print data=derived;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Sep 2024 13:15:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-sequence-id-within-group-and-order-by-date/m-p/944409#M370025</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-09-18T13:15:44Z</dc:date>
    </item>
  </channel>
</rss>

