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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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