<?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: Creating a unique ID that is grouped by levels of another variable only when non-missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/711105#M218989</link>
    <description>Thank you!!!!</description>
    <pubDate>Wed, 13 Jan 2021 14:14:14 GMT</pubDate>
    <dc:creator>claremc</dc:creator>
    <dc:date>2021-01-13T14:14:14Z</dc:date>
    <item>
      <title>Creating a unique ID that is grouped by levels of another variable only when non-missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/710854#M218895</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a unique ID variable for each observation in my dataset. However, I want rows that have the same "DOB_name" value to have the same ID. When the "DOB_name" field is missing, I want the unique ID to be different for each of those rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example of my input data and what I want the ID variable to be:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
input  DOB_name ID
datalines;
24APR2007_JOHN 1 
07FEB2009_SUSIE 2
30DEC2002_MACIE 3
30DEC2002_MACIE 3
. 4
. 5
. 6
;
run;&lt;/PRE&gt;&lt;P&gt;Thanks for your help,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Clare&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 16:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/710854#M218895</guid>
      <dc:creator>claremc</dc:creator>
      <dc:date>2021-01-12T16:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a unique ID that is grouped by levels of another variable only when non-missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/710856#M218897</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
by dob_name notsorted;
 if missing(dob_name) or first.dobname then id+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jan 2021 16:56:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/710856#M218897</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-12T16:56:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a unique ID that is grouped by levels of another variable only when non-missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/710857#M218898</link>
      <description>&lt;P&gt;Your data WANT step doesn't read the data you provide, so I have created data set HAVE from sashelp.class, with a few recurring NAME values.&amp;nbsp; The DATA WANT step shows how to create a hash object (think lookup table stored in memory).&amp;nbsp; When the h.find() method fails, it returns a non-zero, meaning the hash object does not yet have that name. So it retrieves the number of items already in H, adds 1 to generate an ID for the new record, and stores it in H for later access.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if the h.find() is successful (returns a zero), then the ID stored in the hash object has been "retrieved" from h and has set the variable ID to the ID value assigned to the name when it first appeared.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I initially forgot about the need to assign unique ID's to each missing value.&amp;nbsp; The code below has been corrected to fulfill that requirement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note this does NOT need the data to be sorted by the variable used to generate the ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;data have;
  do until (end_of_class);
    set sashelp.class end=end_of_class;
    output;
  end;
  name=' '; output;
  do until (end_of_class2);
    set sashelp.class end=end_of_class2;
    if age=14 then output;
  end;
  name=' '; output;
run;

data want;
  set have;
  if _n_=1 then do;
    declare hash h ();
      h.definekey('name');
      h.definedata('id');
      h.definedone();
  end;
  if h.find()^=0 then do;
    id=h.num_items+1;
    if missing(name) then h.add(key:cats(.,_n_),data:id);
    else h.add();
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 17:09:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/710857#M218898</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-12T17:09:19Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a unique ID that is grouped by levels of another variable only when non-missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/711105#M218989</link>
      <description>Thank you!!!!</description>
      <pubDate>Wed, 13 Jan 2021 14:14:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-that-is-grouped-by-levels-of-another/m-p/711105#M218989</guid>
      <dc:creator>claremc</dc:creator>
      <dc:date>2021-01-13T14:14:14Z</dc:date>
    </item>
  </channel>
</rss>

