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 Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 8482 views
  • 0 likes
  • 3 in conversation