楼主: jasper.jin
1883 6

关于根据时间将变量分组问题 [推广有奖]

  • 1关注
  • 0粉丝

大专生

20%

还不是VIP/贵宾

-

威望
0
论坛币
61 个
通用积分
0
学术水平
3 点
热心指数
5 点
信用等级
5 点
经验
445 点
帖子
24
精华
0
在线时间
46 小时
注册时间
2016-4-11
最后登录
2021-12-29

楼主
jasper.jin 发表于 2016-8-18 14:40:27 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我的数据集a是这样的
name      month
李磊        201510
李磊        201509
张三        201403
李四        201405
张三        201503
李四        201610
李磊        201301

......             ......
我想将 name 分成三类  分类的依据是month   。第一类 A:name 连续两年每个月都有  第二类 B:name在两年内不连续出现   第三类 C :name在最近一年才有 上一年没有  
不知道怎么写 求大神帮助
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:变量分组 month 求大神帮助 name mont 李磊

回帖推荐

wwang111 发表于6楼  查看完整内容

you are correct, sorry for my carelessness, please try this one and take a look at if there's any problem, thanks. proc sort data=a; by name month; run; data consec_month; set a; by name month; mon=month(month); year=year(month); lagmon=lag(mon); lagyear=lag(year); if first.name then call missing(lagmon,lagyear); if year=lagyear then num=mon-lagmon; if year=lagyear+1 and ...

沙发
jasper.jin 发表于 2016-8-18 15:12:18
再补充一下问题的核心点,就是我怎么判断name变量对应的month变量在时间上是连续的

藤椅
wwang111 发表于 2016-8-18 20:58:43
try this:

proc sql;
create table num_month as
select distinct name, year(month) as year, count(*) as n
from a
group by 1,2
order by 1,2;
quit;

data cal_allgrp;
set num_month;
by name year;
lagyear=lag(year);
if name ne lag(name) then do;
   group="C";
   num=0;
end;
else do;
  if year=lagyear+1 then group="B";
  if n=12 then num+1;
  if num>1 and group="B" then group="A";
end;
keep name group;
run;

proc sort data=cal_allgrp;
where group ne '';
by name group;
run;

proc sort data=cal_allgrp out=wanted nodupkey;
by name;
run;

板凳
jasper.jin 发表于 2016-8-22 09:02:09
wwang111 发表于 2016-8-18 20:58
try this:

proc sql;
虽然没太看懂,但是结果出来了,依旧是大神,膜拜

报纸
jasper.jin 发表于 2016-8-25 14:05:36
wwang111 发表于 2016-8-18 20:58
try this:

proc sql;
仔细的看了一遍你的逻辑,时间如果是一年而且取整的话用这个逻辑应该是没错的,如果跨年例如我想要的时间段是14年的3月份 到16年的3月份 那么计数取 12 再往上就+1 变13了 还是没办法判断 两年的连续  不知道我的理解对不对

地板
wwang111 发表于 2016-8-25 15:33:14
you are correct, sorry for my carelessness, please try this one and take a look at if there's any problem, thanks.

proc sort data=a;
by name month;
run;

data consec_month;
set a;
by name month;
mon=month(month);
year=year(month);
lagmon=lag(mon);
lagyear=lag(year);
if first.name then call missing(lagmon,lagyear);
if year=lagyear then num=mon-lagmon;
if year=lagyear+1 and mon=1 and lagmon=12 then num=1;
run;

data getgrp;
set consec_month;
by name month;
if first.name then consnum=0;
if num=1 then consnum+1;
else consnum=0;
group="C";
if year=lag(year)+1 then group="B";
if consnum>=23 then group="A";
keep name group;
run;

proc sort data=getgrp;
by name group;
run;

proc sort data=getgrp out=wanted nodupkey;
by name;
run;

只有一个罗纳尔多

7
jasper.jin 发表于 2016-8-25 16:23:49
wwang111 发表于 2016-8-25 15:33
you are correct, sorry for my carelessness, please try this one and take a look at if there's any pr ...
The result is correct, thank you for your patience and professional solutions

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-21 13:06