在 sas 中计算连续的月份
counting consecutive months in sas
根据下面的示例数据,我正在尝试计算一个会员有多少个连续月份。如果 ID 中间有间隔月,则输出应显示该成员最近的连续月数,请参见下面的示例。
数据集=一个
ID
Month
72
01SEP2020
72
01OCT2020
72
01NOV2020
72
01DEC2020
72
01FEB2021
72
01MAR2021
72
01APR2021
72
01MAY2021
72
01JUN2021
期望的输出如下:
ID
months_ct
72
5
怎么样
data have;
input id month : date9.;
datalines;
72 01SEP2020
72 01OCT2020
72 01NOV2020
72 01DEC2020
72 01FEB2021
72 01MAR2021
72 01APR2021
72 01MAY2021
72 01JUN2021
;
data want;
do until (last.id);
set have;
by id;
if first.id=1 or intck('month',lag(month),month)^=1 then consec_months=1;
else consec_months + 1;
end;
run;
根据下面的示例数据,我正在尝试计算一个会员有多少个连续月份。如果 ID 中间有间隔月,则输出应显示该成员最近的连续月数,请参见下面的示例。
数据集=一个
ID | Month |
---|---|
72 | 01SEP2020 |
72 | 01OCT2020 |
72 | 01NOV2020 |
72 | 01DEC2020 |
72 | 01FEB2021 |
72 | 01MAR2021 |
72 | 01APR2021 |
72 | 01MAY2021 |
72 | 01JUN2021 |
期望的输出如下:
ID | months_ct |
---|---|
72 | 5 |
怎么样
data have;
input id month : date9.;
datalines;
72 01SEP2020
72 01OCT2020
72 01NOV2020
72 01DEC2020
72 01FEB2021
72 01MAR2021
72 01APR2021
72 01MAY2021
72 01JUN2021
;
data want;
do until (last.id);
set have;
by id;
if first.id=1 or intck('month',lag(month),month)^=1 then consec_months=1;
else consec_months + 1;
end;
run;