<?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 Create new ID starting from 1 in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Create-new-ID-starting-from-1/m-p/510590#M1985</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a longitudinal data where each subject has a number of repeated measurements, i.e., occupying several rows in the data table. Unfortunately, the ID is not starting from 1. Now I would like to create a new ID, starting from 1. I have completed a SAS code as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data original;
input   ID  ;
cards;
12
12
12
16
16
19
19
19
27
27
27
;
run;

data newID;
      set original;
      retain newID 1 previousID 12;
      if ID=previousID then newID=newID;
      else newID=newID+1;
      previousID=ID;
	  drop previousID;
run;

Proc print data=newID noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This code works as I want. However, I feel the code complicated. Do you have any suggestion to make it simpler?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for reading!&lt;/P&gt;</description>
    <pubDate>Mon, 05 Nov 2018 20:35:15 GMT</pubDate>
    <dc:creator>trungdungtran</dc:creator>
    <dc:date>2018-11-05T20:35:15Z</dc:date>
    <item>
      <title>Create new ID starting from 1</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-new-ID-starting-from-1/m-p/510590#M1985</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a longitudinal data where each subject has a number of repeated measurements, i.e., occupying several rows in the data table. Unfortunately, the ID is not starting from 1. Now I would like to create a new ID, starting from 1. I have completed a SAS code as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data original;
input   ID  ;
cards;
12
12
12
16
16
19
19
19
27
27
27
;
run;

data newID;
      set original;
      retain newID 1 previousID 12;
      if ID=previousID then newID=newID;
      else newID=newID+1;
      previousID=ID;
	  drop previousID;
run;

Proc print data=newID noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This code works as I want. However, I feel the code complicated. Do you have any suggestion to make it simpler?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for reading!&lt;/P&gt;</description>
      <pubDate>Mon, 05 Nov 2018 20:35:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-new-ID-starting-from-1/m-p/510590#M1985</guid>
      <dc:creator>trungdungtran</dc:creator>
      <dc:date>2018-11-05T20:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create new ID starting from 1</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-new-ID-starting-from-1/m-p/510593#M1987</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/199021"&gt;@trungdungtran&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a longitudinal data where each subject has a number of repeated measurements, i.e., occupying several rows in the data table. Unfortunately, the ID is not starting from 1. Now I would like to create a new ID, starting from 1. I have completed a SAS code as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data original;
input   ID  ;
cards;
12
12
12
16
16
19
19
19
27
27
27
;
run;

data newID;
      set original;
      retain newID 1 previousID 12;
      if ID=previousID then newID=newID;
      else newID=newID+1;
      previousID=ID;
	  drop previousID;
run;

Proc print data=newID noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code works as I want. However, I feel the code complicated. Do you have any suggestion to make it simpler?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for reading!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Something like this maybe?&lt;/P&gt;
&lt;PRE&gt;data have;
input   ID  ;
cards;
12
12
12
16
16
19
19
19
27
27
27
;
run;
proc sort data=have;
  by id;
run;

data want;
   set have;
   by id;
   if first.id then Newid+1;
run;
&lt;/PRE&gt;
&lt;P&gt;I explicitly sorted the data by ID just in case and so that all of the values of ID end up with the same newid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Nov 2018 20:47:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-new-ID-starting-from-1/m-p/510593#M1987</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-11-05T20:47:57Z</dc:date>
    </item>
  </channel>
</rss>

