BookmarkSubscribeRSS Feed
Nupur20
Calcite | Level 5

I have a dataset with variables as ID and Diagnosis. I want to assign serial number by making a new column (say Diagnosis_serial_number) to diagnosis based on ID. For eg: If ID 1 has 5 diagnosis, I want serial numbers to be (1, 2, 3, 4,5) and then if ID 2 has 3 diagnosis, the serial numbers would be (1, 2,3) and so on.

What the code to do that in SAS?

I tried this:

data = temp;

set data1;

diagnosis = _N_;

run;

It just assigned continuous serial number but not based on different IDs.

I would appreciate your suggestions. Thanks so much.

2 REPLIES 2
gxu
Calcite | Level 5 gxu
Calcite | Level 5

Try this:

data serial;

input id diag;

cards;

1 101

1 102

1 103

2 201

2 202

2 203

3 301

3 302

;

run;

data serial2;

set serial;

by id;

if first.id then diag_id=0;

diag_id+1;

run;

********************

the dataset serial2 looks like the following, is this what you are expecting?

id    diag    diag_id

1     101       1

1     102       2

1     103       3

2     201       1

2     202       2

2     203       3

3     301       1

3     302       2

art297
Opal | Level 21

Of course, to run the code suggested by gxu, your data first has to be sorted by id.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 8578 views
  • 0 likes
  • 3 in conversation