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.
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
Of course, to run the code suggested by gxu, your data first has to be sorted by id.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.