<?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: How to assign a unique ID number to each row with identical variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-unique-ID-number-to-each-row-with-identical/m-p/832919#M329253</link>
    <description>&lt;P&gt;First, make sure that your example data step runs. No format assigned for DOB means that you have invalid data on each record. Also the semicolon that ends the datalines block must be on a separate line.&lt;/P&gt;
&lt;P&gt;Second, make sure that the INPUT data matches the OUTPUT, you did not include that M record in the input;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;DATA outdata;
INPUT First_Name $ Last_Name $ DOB :mmddyy10. Gender$;
format dob mmddyy10.;
DATALINES;
TestF TestL 3/3/2010 F
TestF TestL 3/3/2010 F
TestF1 TestL1 3/9/2010 F
TestF1 TestL1 3/9/2010 F
TestF TestL 3/3/2010 F
TestF1 TestL1 3/9/2010 M 
;

run;

Proc sort data=outdata;
   by last_name first_name dob gender;
run;

data want;
   set outdata;
   retain uniqid 0;
   by last_name first_name dob gender;
   if first.gender then uniqid+1;
run;
&lt;/PRE&gt;
&lt;P&gt;BY group processing in a data step establishes automatic variables that indicate whether the current value is the first or last of the group. Those are accessed with First.variablename or Last.variablename. The values are 1 or 0 when "true", i.e. the first or the last, and SAS will use those as logical true/false. The Retain establishes a variable that persists across data step boundaries so that once the value is assigned it is kept until changed. BY group processing for this process would want the data to be in order to keep all the identity&amp;nbsp; variables together, hence the sort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the output order of the data set is critical, state so but it is relatively trivial to add an order variable(before the sort) and resort the data back to that order.&lt;/P&gt;</description>
    <pubDate>Mon, 12 Sep 2022 15:17:01 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-09-12T15:17:01Z</dc:date>
    <item>
      <title>How to assign a unique ID number to each row with identical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-unique-ID-number-to-each-row-with-identical/m-p/832912#M329249</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a dataset with variables First_Name Last_Name DOB Gender.&amp;nbsp; I am trying&amp;nbsp;to create a unique ID for rows with identical&amp;nbsp;First_Name Last_Name DOB and&amp;nbsp; Gender&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA outdata;&lt;BR /&gt;INPUT &lt;SPAN&gt;First_Name $ Last_Name $ DOB Gender$&lt;/SPAN&gt;;&lt;BR /&gt;DATALINES;&lt;BR /&gt;TestF TestL 3/3/2010 F&lt;BR /&gt;TestF TestL 3/3/2010 F&lt;BR /&gt;TestF1 TestL1 3/9/2010 F&lt;BR /&gt;TestF1 TestL1 3/9/2010 F&lt;BR /&gt;TestF TestL 3/3/2010 F;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;desired output:&lt;/P&gt;&lt;P&gt;data&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;uniqid&lt;/P&gt;&lt;P&gt;=======&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; =====&lt;/P&gt;&lt;P&gt;TestF TestL 3/3/2010 F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;TestF TestL 3/3/2010 F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;TestF1 TestL1 3/9/2010 F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;TestF1 TestL1 3/9/2010 F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;TestF TestL 3/3/2010 F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;TestF1 TestL1 3/9/2010 M&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 14:59:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-unique-ID-number-to-each-row-with-identical/m-p/832912#M329249</guid>
      <dc:creator>sasuser123</dc:creator>
      <dc:date>2022-09-12T14:59:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a unique ID number to each row with identical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-unique-ID-number-to-each-row-with-identical/m-p/832917#M329251</link>
      <description>&lt;P&gt;You can sort so that all records with identical First_Name Last_Name DOB Gender are together, then this can be accomplished as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=outdata;
    by first_name last_name dob gender;
run;

data want;
    set outdata;
    by first_name last_name dob gender;
    if first.gender then uniqid+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 15:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-unique-ID-number-to-each-row-with-identical/m-p/832917#M329251</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-09-12T15:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign a unique ID number to each row with identical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-unique-ID-number-to-each-row-with-identical/m-p/832919#M329253</link>
      <description>&lt;P&gt;First, make sure that your example data step runs. No format assigned for DOB means that you have invalid data on each record. Also the semicolon that ends the datalines block must be on a separate line.&lt;/P&gt;
&lt;P&gt;Second, make sure that the INPUT data matches the OUTPUT, you did not include that M record in the input;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;DATA outdata;
INPUT First_Name $ Last_Name $ DOB :mmddyy10. Gender$;
format dob mmddyy10.;
DATALINES;
TestF TestL 3/3/2010 F
TestF TestL 3/3/2010 F
TestF1 TestL1 3/9/2010 F
TestF1 TestL1 3/9/2010 F
TestF TestL 3/3/2010 F
TestF1 TestL1 3/9/2010 M 
;

run;

Proc sort data=outdata;
   by last_name first_name dob gender;
run;

data want;
   set outdata;
   retain uniqid 0;
   by last_name first_name dob gender;
   if first.gender then uniqid+1;
run;
&lt;/PRE&gt;
&lt;P&gt;BY group processing in a data step establishes automatic variables that indicate whether the current value is the first or last of the group. Those are accessed with First.variablename or Last.variablename. The values are 1 or 0 when "true", i.e. the first or the last, and SAS will use those as logical true/false. The Retain establishes a variable that persists across data step boundaries so that once the value is assigned it is kept until changed. BY group processing for this process would want the data to be in order to keep all the identity&amp;nbsp; variables together, hence the sort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the output order of the data set is critical, state so but it is relatively trivial to add an order variable(before the sort) and resort the data back to that order.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 15:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-a-unique-ID-number-to-each-row-with-identical/m-p/832919#M329253</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-12T15:17:01Z</dc:date>
    </item>
  </channel>
</rss>

