<?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 for multiple repeating variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741931#M232003</link>
    <description>Please supply a have data set. I'm assuming your have data set has additional variables and not just date_reported_f. You will need a variable (or rule) that will tell the ID variable you want to create to increment.</description>
    <pubDate>Mon, 17 May 2021 17:16:24 GMT</pubDate>
    <dc:creator>AMSAS</dc:creator>
    <dc:date>2021-05-17T17:16:24Z</dc:date>
    <item>
      <title>Creating a unique ID for multiple repeating variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741926#M232002</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 based on 2 variables (record ID, and a date). The unique variable must restart at 1 whenever the record ID changes. The record ID can also be repeating for some records.&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example, here is the data I have with the ID that I want:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; data WORK.CLASS(label='Student Data');
   infile datalines;
   input record_id date_reported; 
 datalines;
1 04/06/2020
1 05/06/2020
2 03/06/2021
2 04/07/2021
3 07/09/2021
4 02 06/2021
 ;
run;&lt;/PRE&gt;&lt;P&gt;I have tried this but did not get the ID to repeat:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
 set have;
     by record_id date_reported_f/* if need sort data have */;
          retain ID 0;
          if first.date_reported_f then ID+1;
run;&lt;/PRE&gt;&lt;P&gt;Let me know if you have any suggestions!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Clare&lt;/P&gt;</description>
      <pubDate>Mon, 17 May 2021 16:59:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741926#M232002</guid>
      <dc:creator>claremc</dc:creator>
      <dc:date>2021-05-17T16:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a unique ID for multiple repeating variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741931#M232003</link>
      <description>Please supply a have data set. I'm assuming your have data set has additional variables and not just date_reported_f. You will need a variable (or rule) that will tell the ID variable you want to create to increment.</description>
      <pubDate>Mon, 17 May 2021 17:16:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741931#M232003</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-05-17T17:16:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a unique ID for multiple repeating variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741938#M232005</link>
      <description>&lt;P&gt;It is a little hard to interpret what you want.&amp;nbsp; If you want a new identifier that counts the observations per REPORT_ID then perhaps something like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
   input record_id date_reported :mmddyy.;
   format date_reported yymmdd10.; 
datalines;
1 04/06/2020
1 05/06/2020
2 03/06/2021
2 04/07/2021
3 07/09/2021
4 02/06/2021
;

data want;
  set have;
  by record_id date_reported;
  subid + 1;
  if first.record_id then subid=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;       record_         date_
Obs       id        reported    subid

 1        1       2020-04-06      1
 2        1       2020-05-06      2
 3        2       2021-03-06      1
 4        2       2021-04-07      2
 5        3       2021-07-09      1
 6        4       2021-02-06      1

&lt;/PRE&gt;
&lt;P&gt;If instead you want the same ID*DATE_REPORTED values to have the same SUBID then we need to change the code (and change the test data since the current data the results would be exactly the same since no dates repeat).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
   input record_id date_reported :mmddyy.;
   format date_reported yymmdd10.; 
datalines;
1 04/06/2020
1 05/06/2020
1 05/06/2020
1 05/06/2020
2 03/06/2021
2 04/07/2021
3 07/09/2021
4 02/06/2021
;

data want;
  set have;
  by record_id date_reported;
  subid + first.date_reported;
  if first.record_id then subid=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;       record_         date_
Obs       id        reported    subid

 1        1       2020-04-06      1
 2        1       2020-05-06      2
 3        1       2020-05-06      2
 4        1       2020-05-06      2
 5        2       2021-03-06      1
 6        2       2021-04-07      2
 7        3       2021-07-09      1
 8        4       2021-02-06      1
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 May 2021 17:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741938#M232005</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-17T17:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a unique ID for multiple repeating variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741947#M232011</link>
      <description>Thank you so much! Sorry for the confusion. The first example was what I wanted - a new identifier that counts the observations per REPORT_ID. So helpful, thanks again!</description>
      <pubDate>Mon, 17 May 2021 18:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-unique-ID-for-multiple-repeating-variables/m-p/741947#M232011</guid>
      <dc:creator>claremc</dc:creator>
      <dc:date>2021-05-17T18:13:08Z</dc:date>
    </item>
  </channel>
</rss>

