BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
trungdungtran
Obsidian | Level 7

Hi all,

 

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:

 

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;

This code works as I want. However, I feel the code complicated. Do you have any suggestion to make it simpler?

 

Thank you for reading!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@trungdungtran wrote:

Hi all,

 

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:

 

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;

This code works as I want. However, I feel the code complicated. Do you have any suggestion to make it simpler?

 

Thank you for reading!


Something like this maybe?

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;

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.

 

View solution in original post

1 REPLY 1
ballardw
Super User

@trungdungtran wrote:

Hi all,

 

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:

 

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;

This code works as I want. However, I feel the code complicated. Do you have any suggestion to make it simpler?

 

Thank you for reading!


Something like this maybe?

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;

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.

 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1800 views
  • 0 likes
  • 2 in conversation