楼主: claudiehuang
1545 13

[问答] 求助,如何分组给每组里面的数据编号 [推广有奖]

11
superguy333 发表于 2021-2-16 05:48:34 |只看作者 |坛友微信交流群
claudiehuang 发表于 2021-2-14 09:04
大神们拜托看一看~
/*录入数据*/
data x1;
input personid 3.0  year_month $10.;
cards;
1  199001
1  199002
1  199005
1  199007
1  199008
1  199210
1  199212
1  199301
1  199302
2  199003
2  199004
2  199006
2  199202
2  199204
;
run;

/*排序*/
proc sort data =x1;
by personid  year_month;
run;

/*计算月差month_diff*/
data x2;
set x1;
year_month1=lag(year_month);
month_diff=(substr(year_month,1,4)-substr(year_month1,1,4))*12+substr(year_month,5,2)-substr(year_month1,5,2);  
run;

/*retain语句 定义变量seq,1、首次出现personid时,seq=1;
                          2、非首次出现personid并且月差≥3时,seq自动增加1*/
data x3;
set x2;
retain seq ;
by personid;
if first.personid then seq=1;
if not first.personid and month_diff>=3 then seq=seq+1;
DROP year_month1 month_diff;
run;

使用道具

12
Lee_iris 在职认证  学生认证  发表于 2021-2-16 10:46:16 |只看作者 |坛友微信交流群
最近审核变得好慢啊

使用道具

13
school 在职认证  发表于 2021-2-16 11:29:18 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-24 10:15