Help using Base SAS procedures

Assigning serial numbers to a column in sas

Reply
Contributor
Posts: 33

Assigning serial numbers to a column in sas

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.

Occasional Contributor gxu
Occasional Contributor
Posts: 18

Assigning serial numbers to a column in sas

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

PROC Star
Posts: 7,363

Assigning serial numbers to a column in sas

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

Ask a Question
Discussion stats
  • 2 replies
  • 2552 views
  • 0 likes
  • 3 in conversation