BookmarkSubscribeRSS Feed
dapenDaniel
Obsidian | Level 7

Hello,

 

I have a dataset as below.

 

ID    V1      V2

1     aa       bb

1     aa       bc

1     aa       bd

 

I want to add a second ID as a separate variable which begins from 751. Both ID and ID2 are numeric variables. The dataset I want is below.

 

ID   V1      V2    ID2

1     aa       bb   751

1     aa       bc   751

1     aa       bd   751

2     bb       cc   752

3     ae       dg   753

 

what code do I need to use? Thanks!

3 REPLIES 3
ballardw
Super User

@dapenDaniel wrote:

Hello,

 

I have a dataset as below.

 

ID    V1      V2

1     aa       bb

1     aa       bc

1     aa       bd

 

I want to add a second ID which begins from 751. The dataset I want is below.

 

 

 

 

what code do I need to use? Thanks!


Do you want to prefix your current ID variable with 75 or create a new sequence that starts at 751? The difference would be

 

ID   V1      V2    ID2
1     aa       bb   751
1     aa       bc   751
1     aa       bd   751
2     bb       cc   752
3     ae       dg   753
5     cc       dd   755 (prefix) or 754 (sequence)

Also is your ID character or numeric? And do you want the ID2 to be character or numeric?

dapenDaniel
Obsidian | Level 7

Hi Ballardw,

 

I would like to create a new sequence that starts at 751.

 

Both ID and ID2 are numeric variables. Thanks.

Reeza
Super User

Assuming you only want to increment based on ID1, you can use RETAIN and BY group processing. 

 

data want;
set have;

by ID1;  

retain ID2 750;

if first.ID1 then ID2 + 1;

run;

BY group processing is covered here:

https://documentation.sas.com/?docsetId=lrcon&docsetTarget=p0xu93fy5eemkyn1p6mj5elses7j.htm&docsetVe...

 


@dapenDaniel wrote:

Hello,

 

I have a dataset as below.

 

ID    V1      V2

1     aa       bb

1     aa       bc

1     aa       bd

 

I want to add a second ID as a separate variable which begins from 751. Both ID and ID2 are numeric variables. The dataset I want is below.

 

ID   V1      V2    ID2

1     aa       bb   751

1     aa       bc   751

1     aa       bd   751

2     bb       cc   752

3     ae       dg   753

 

what code do I need to use? Thanks!


 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 3 replies
  • 3433 views
  • 2 likes
  • 3 in conversation