我有一个数据集,其变量为ID和Diagnosis。我想根据ID将序列numnber分配给诊断。 例如:如果ID 1有5个诊断,我希望序列号为(1,2,3,4,5),如果ID 2有3个诊断,序列号将是(1,2,3),所以上。
在SAS中有没有办法做到这一点? 非常感谢。
嗨非常感谢你的回复。我其实想要这样。Obs diagnosis id diagnosis_serial_number
1 XX 1 1
2 XX1 1 2
3 XX2 1 3
4 XX3 1 4
5 XX4 1 5
6 XX 2 1
7 XX1 2 2
8 XX2 2 3
答案 0 :(得分:2)
我对你究竟要求的内容有点不清楚,但我认为这可能会成功。它使用retain语句和增量步骤来创建一个名为serial
的计数器变量,并在每次找到新的ID组时使用组处理来重置计数器。
请注意,在使用此代码之前,您的输入数据集(此处称为input_ds
)必须先按ID排序。
data output_ds;
retain serial;
set input_ds;
by ID;
if first.ID then serial = 0;
serial = serial + 1;
run;
答案 1 :(得分:1)
Nupur,为了得到你刚才描述的输出,然后试试这个:
proc sort data=temp;
by id diagnosis;
run;
data temp;
set temp;
by id diagnosis;
retain diagnosis_serial_number 0;
if first.id then do;
diagnosis_serial_number = 0;
end;
diagnosis_serial_number + 1;
run;
答案 2 :(得分:0)
Nupur,如果您需要获得每个诊断的序列号,请尝试以下方法:
proc sort data=temp;
by id diagnosis;
run;
data temp_serial_number;
set temp;
by id diagnosis;
format diagnosis_serial_number $30.;
retain diagnosis_serial_number count;
if first.id then do;
count = 0;
diagnosis_serial_number = "(";
end;
count + 1;
diagnosis_serial_number = trim(left(diagnosis_serial_number)) || trim(left(count));
if last.id then do;
diagnosis_serial_number = trim(left(diagnosis_serial_number)) || ")";
output;
end;
else do;
diagnosis_serial_number = trim(left(diagnosis_serial_number)) || ",";
end;
drop diagnosis count;
run;
data temp;
merge temp(in=in_temp) temp_serial_number(in=in_serial_nbr);
by id;
if in_temp then output;
run;
proc print data=temp;
run;
输出如下:
Obs diagnosis id diagnosis_serial_number
1 XX 1 (1,2,3,4,5)
2 XX1 1 (1,2,3,4,5)
3 XX2 1 (1,2,3,4,5)
4 XX3 1 (1,2,3,4,5)
5 XX4 1 (1,2,3,4,5)
6 XX 2 (1,2,3)
7 XX1 2 (1,2,3)
8 XX2 2 (1,2,3)