<?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: Flag multiple duplicates (without deleting) so third duplicate has different flag from second in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722689#M80236</link>
    <description>&lt;P&gt;Thank you, this is exactly what I needed! And yes you are correct -- that was a typo in my set statement.&lt;/P&gt;</description>
    <pubDate>Mon, 01 Mar 2021 20:49:27 GMT</pubDate>
    <dc:creator>MHines</dc:creator>
    <dc:date>2021-03-01T20:49:27Z</dc:date>
    <item>
      <title>Flag multiple duplicates (without deleting) so third duplicate has different flag from second</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722681#M80233</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am analyzing a dataset with multiple duplicate ID numbers in it and am trying to create a flag such that, if an ID appears four times for example, the first appearance is flagged 0, second is flagged 1, third is flagged 2, and fourth is flagged 3. I have approximately 139,000 unique IDs and 356,000 rows total. About 40% of my IDs are unique, but the number of observations per ID ranges from 1 to 98 (I only really care about the first three observations per ID, but I need to be able to differentiate between the second and third). I successfully created a flag that identifies the duplicates, but it does not differentiate between the second and all duplicates following it (it's just a 0 /1 binary indicator currently, where =1 tells me the row is a duplicate). Here is my current code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set data have;&lt;/P&gt;&lt;P&gt;by idnumber;&lt;/P&gt;&lt;P&gt;if first.idnumber=0 or last.idnumber=0 then dupflag = 1;&lt;/P&gt;&lt;P&gt;else flag=0;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any assistance is appreciated. Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 20:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722681#M80233</guid>
      <dc:creator>MHines</dc:creator>
      <dc:date>2021-03-01T20:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: Flag multiple duplicates (without deleting) so third duplicate has different flag from second</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722685#M80234</link>
      <description>&lt;P&gt;You can use the SUM statement, which has an implicit retain. The retain forces SAS to keep the values across rows and continuously add to it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - I'm assuming your SET statement is incorrect and DATA isn't required.&lt;/P&gt;
&lt;P&gt;Full details are available here if you're not sure how this works:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;

set have;

by idnumber;

*not required as sum does it implicitly;
retain counter;

if first.idnumber then counter=0;
else counter+1;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/362272"&gt;@MHines&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am analyzing a dataset with multiple duplicate ID numbers in it and am trying to create a flag such that, if an ID appears four times for example, the first appearance is flagged 0, second is flagged 1, third is flagged 2, and fourth is flagged 3. I have approximately 139,000 unique IDs and 356,000 rows total. About 40% of my IDs are unique, but the number of observations per ID ranges from 1 to 98 (I only really care about the first three observations per ID, but I need to be able to differentiate between the second and third). I successfully created a flag that identifies the duplicates, but it does not differentiate between the second and all duplicates following it (it's just a 0 /1 binary indicator currently, where =1 tells me the row is a duplicate). Here is my current code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set data have;&lt;/P&gt;
&lt;P&gt;by idnumber;&lt;/P&gt;
&lt;P&gt;if first.idnumber=0 or last.idnumber=0 then dupflag = 1;&lt;/P&gt;
&lt;P&gt;else flag=0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any assistance is appreciated. Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 20:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722685#M80234</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-01T20:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Flag multiple duplicates (without deleting) so third duplicate has different flag from second</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722687#M80235</link>
      <description>&lt;P&gt;Why not just index all starting at 0.&amp;nbsp; If you don't care about anything past FLAG=3 it won't matter what value the flag has.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/362272"&gt;@MHines&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am analyzing a dataset with multiple duplicate ID numbers in it and am trying to create a flag such that, if an ID appears four times for example, the first appearance is flagged 0, second is flagged 1, third is flagged 2, and fourth is flagged 3. I have approximately 139,000 unique IDs and 356,000 rows total. About 40% of my IDs are unique, but the number of observations per ID ranges from 1 to 98 (I only really care about the first three observations per ID, but I need to be able to differentiate between the second and third). I successfully created a flag that identifies the duplicates, but it does not differentiate between the second and all duplicates following it (it's just a 0 /1 binary indicator currently, where =1 tells me the row is a duplicate). Here is my current code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set data have;&lt;/P&gt;
&lt;P&gt;by idnumber;&lt;/P&gt;
&lt;P&gt;if first.idnumber=0 or last.idnumber=0 then dupflag = 1;&lt;/P&gt;
&lt;P&gt;else flag=0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any assistance is appreciated. Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 20:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722687#M80235</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2021-03-01T20:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: Flag multiple duplicates (without deleting) so third duplicate has different flag from second</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722689#M80236</link>
      <description>&lt;P&gt;Thank you, this is exactly what I needed! And yes you are correct -- that was a typo in my set statement.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 20:49:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Flag-multiple-duplicates-without-deleting-so-third-duplicate-has/m-p/722689#M80236</guid>
      <dc:creator>MHines</dc:creator>
      <dc:date>2021-03-01T20:49:27Z</dc:date>
    </item>
  </channel>
</rss>

