如何获得每个组的识别号码?

时间:2017-09-07 06:14:05

标签: sorting date variables filter sas

我想要以下表格,

stnd_y person_id recu_day date      sick_sym   Admission

2002   100       20020929 02-09-29  A          1
2002   100       20020929 02-09-29  B          1
2002   100       20020929 02-09-29  D          1
2002   100       20020930 02-09-30  B          2
2002   100       20020930 02-09-30  E          2
2002   100       20021002 02-10-02  X          3
2002   100       20021002 02-10-02  W          3
2002   101       20020927 02-09-27  S          1
2002   101       20020927 02-09-27  O          1
2002   101       20020928 02-09-28  C          2
2002   102       20021001 02-10-01  F          1
2002   103       20021003 02-10-03  G          1
2002   104       20021108 02-11-08  H          1
2002   104       20021108 02-11-08  A          1
2002   104       20021112 02-11-12  B          2


proc sort data=a out=a1;
by person_id recu_fr_dt;
data a3;
set a1 ;
by person_id recu_fr_dt;
if first.person_id then adm+1;
run;

根据上述代码,结果如下,因为我不是这个意思。

stnd_y person_id recu_day date      sick_sym   Admission

2002   100       20020929 02-09-29  A          1
2002   100       20020929 02-09-29  B          2
2002   100       20020929 02-09-29  D          3
2002   100       20020930 02-09-30  B          4
2002   100       20020930 02-09-30  E          5
2002   100       20021002 02-10-02  X          6
2002   100       20021002 02-10-02  W          7
2002   101       20020927 02-09-27  S          1
2002   101       20020927 02-09-27  O          2
2002   101       20020928 02-09-28  C          3
2002   102       20021001 02-10-01  F          1
2002   103       20021003 02-10-03  G          1
2002   104       20021108 02-11-08  H          1
2002   104       20021108 02-11-08  A          2
2002   104       20021112 02-11-12  B          3

同样,我使用了以下的sas,

proc sort data=old out=new;
by person_id recu_day;
data new1;
set new;
retain admission 0;
by person_id recu_day;
if recu_day^=lag(recu_day) and(or) person_id^=lag(person_id) then 
admission+1;
run;

data new1;
set new ;
by person_id recu_day;
retain adm 0;
if first.person_id and(or) first.recu_day then admission=admission+1;
run;

但是,那些不起作用。我怎么解决这个问题?请让我知道这件事。

谢谢

我该怎么办呢?

谢谢! :d

请访问http://stackoverflow.com/questions/46076468/how-can-i-get-the-identification-number-with-each-groups/

1 个答案:

答案 0 :(得分:0)

以下是对我之前问题的回答的修改。

这次,每当给定person_id的日期发生变化时,它会向adm变量添加1。 retain语句确保为peson_id和recu_day相同的所有后续行复制当前值。

data have;
input stnd_y person_id recu_day date :yymmdd8. sick_sym $ Admission;
datalines;
2002   100       20020929 02-09-29  A          1
2002   100       20020929 02-09-29  B          1
2002   100       20020929 02-09-29  D          1
2002   100       20020930 02-09-30  B          2
2002   100       20020930 02-09-30  E          2
2002   100       20021002 02-10-02  X          3
2002   100       20021002 02-10-02  W          3
2002   101       20020927 02-09-27  S          1
2002   101       20020927 02-09-27  O          1
2002   101       20020928 02-09-28  C          2
2002   102       20021001 02-10-01  F          1
2002   103       20021003 02-10-03  G          1
2002   104       20021108 02-11-08  H          1
2002   104       20021108 02-11-08  A          1
2002   104       20021112 02-11-12  B          2
;
run;

data want;
set have;
by person_id recu_day;
retain adm;
if first.person_id then adm=0;
if first.recu_day then adm+1;
run;