ID的SAS序列号
我有这样一个数据集:ID的SAS序列号,sas,Sas,我有这样一个数据集: ID_no | Medication_type 1 | type_1 1 | type_2 2 | type_1 3 | type_2 3 | type_3 3 | type_4 data want; set have; by ID_no; if first.ID_no then do; seq_no = 1; seq_no + 1
ID_no | Medication_type
1 | type_1
1 | type_2
2 | type_1
3 | type_2
3 | type_3
3 | type_4
data want;
set have;
by ID_no;
if first.ID_no then do;
seq_no = 1;
seq_no + 1;
end;
run;
我想把它转换成一组
ID_NO | Medication 1 | Medication2 | Medication 3
1 | type_1 | type 2 |
2 | Type_1 | |
3 | Type_2 | Type_3 | Type_4
要做到这一点,我需要做一个proc转置,但我想我需要一个像这样的序列号才能到达:
ID_no | Medication_type | Seq_NO
1 | type_1 | 1
1 | type_2 | 2
2 | type_1 | 1
3 | type_2 | 1
3 | type_3 | 2
3 | type_4 | 3
但不幸的是,我在sas很难做到这一点。我试着这样做:
ID_no | Medication_type
1 | type_1
1 | type_2
2 | type_1
3 | type_2
3 | type_3
3 | type_4
data want;
set have;
by ID_no;
if first.ID_no then do;
seq_no = 1;
seq_no + 1;
end;
run;
但是它一直在计算,我不知道为什么?您已经有了一个用于分组的变量,您不需要另一个变量
proc transpose data=have out=want prefix=medication_ ;
by id_no;
var medication_type;
run;
至于创建SEQ_NO变量的尝试,您需要将sum语句移出IF块,以便它对每个观察都运行。像这样的
data want;
set have;
by ID_no ;
if first.ID_no then seq_no=0;
seq_no+1;
run;