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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 2085 views
  • 0 likes
  • 2 in conversation