楼主: lachance
9218 9

[原创博文] 如何取出一列的第一条和最后一条记录,存放在变量中? [推广有奖]

  • 1关注
  • 3粉丝

已卖:3份资源

讲师

44%

还不是VIP/贵宾

-

威望
0
论坛币
693 个
通用积分
0.0157
学术水平
4 点
热心指数
5 点
信用等级
1 点
经验
10072 点
帖子
403
精华
0
在线时间
399 小时
注册时间
2008-1-22
最后登录
2020-4-15

楼主
lachance 发表于 2011-2-11 22:36:15 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
DATASET 可以简化如下,ID startmonth
1        198011
1        198012
1        198101
1        198102
1        198103
2        201101
2        201102
2        201103
2        201104

请问如何获得当ID=1 或者 2 时, STARTMONTH列的第一个条和最后一条记录集: 也就是当ID=1时 , STARTMONTH 的第一条记录(198011) 和最后一条记录(198103)都可以取出放在变量中;对ID=2也是进行同样的操作。主要是想根据ID 分类,来计算每组的月份差 ,因为我的时间有不连续情况存在。
多谢

二维码

扫码加我 拉你入群

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

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

关键词:dataset month Start DataS mont 记录 如何

回帖推荐

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

1# lachance new是个新的dataset,看起来像是达到你的要求了

本帖被以下文库推荐

沙发
crackman 发表于 2011-2-11 22:42:58
用first.var or last.var即可

藤椅
baoaibaobao 发表于 2011-2-11 22:46:22
2# crackman 又见版主魅影~~呵呵!

板凳
798841612 发表于 2011-2-11 22:46:48
、、、、、、、、、、、、

报纸
lachance 发表于 2011-2-12 00:28:29
这个只是个布尔值,我想把id =1 or id =2 时 STARTMONTH列的第一个日期和最后一个日期都取出来,放在变量里。
crackman 发表于 2011-2-11 22:42
用first.var or last.var即可
[

地板
junuylia 发表于 2011-2-12 04:20:31
1# lachance
new是个新的dataset,看起来像是达到你的要求了
  1. data test;
  2. input ID startmonth;
  3. cards;
  4. 1        198011
  5. 1        198012
  6. 1        198101
  7. 1        198102
  8. 1        198103
  9. 2        201101
  10. 2        201102
  11. 2        201103
  12. 2        201104
  13. ;
  14. run;

  15. proc sort data=test;
  16. by id;
  17. run;

  18. data startmon(rename=(startmonth=startM)) endmon(rename=(startmonth=endM));
  19. set test;
  20. by id;
  21. if (first.id) then   output startmon;
  22. if (last.id) then output endmon;
  23. run;

  24. data new;
  25. merge startmon endmon;
  26. by id;
  27. run;

  28. proc print data=new;
  29. run;
复制代码
已有 1 人评分论坛币 热心指数 收起 理由
crackman + 20 + 2 鼓励积极发帖讨论

总评分: 论坛币 + 20  热心指数 + 2   查看全部评分

7
hopewell 发表于 2011-2-12 07:49:21
  1. data test;
  2.     input ID startmonth;
  3.     startmonth=input(put(startmonth*100+1,8.),yymmdd8.);
  4.     format startmonth yymms7.;
  5. cards;
  6. 1        198011
  7. 1        198012
  8. 1        198101
  9. 1        198102
  10. 1        198103
  11. 2        201101
  12. 2        201102
  13. 2        201103
  14. 2        201104
  15. ;
  16. data out(drop=startmonth);
  17.     do _n_=1 by 1 until(last.id);
  18.         set test;
  19.         by id notsorted;
  20.         st_month=min(st_month,startmonth);
  21.         ed_month=max(ed_month,startmonth);
  22.     end;
  23.     dif=intck('month',st_month,ed_month);
  24.     format st_month ed_month yymms7.;
  25. run;
复制代码
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
hssnow + 1 + 1 + 1 观点有启发
crackman + 20 + 1 + 2 精彩帖子

总评分: 论坛币 + 20  学术水平 + 2  热心指数 + 3  信用等级 + 1   查看全部评分

观钓颇逾垂钓趣 种花何问看花谁

8
baoaibaobao 发表于 2011-2-12 09:00:34
  1. data test;
  2.     input id startmonth;
  3.     informat startmonth yymmn6.;
  4.     format startmonth yymmn6.;
  5. cards;
  6. 1        198011
  7. 1        198012
  8. 1        198101
  9. 1        198102
  10. 1        198103
  11. 2        201101
  12. 2        201102
  13. 2        201103
  14. 2        201104
  15. ;
  16. proc sql;
  17.     create table zachary as
  18.     select id
  19.               ,min(startmonth) as f_month format=yymmn6.
  20.               ,max(startmonth) as l_month format=yymmn6.
  21.               ,intck('month',calculated f_month,calculated l_month) as month_diff
  22.     from test
  23.     group by id;
  24. quit;
复制代码
已有 1 人评分经验 热心指数 收起 理由
crackman + 20 + 1 鼓励积极发帖讨论

总评分: 经验 + 20  热心指数 + 1   查看全部评分

9
lachance 发表于 2011-2-14 07:11:57
thank you very much, guys.
[

10
elek.me 发表于 2011-2-14 12:31:54
  1. data have;
  2.         input ID startmonth :yymmn6.;
  3. cards;
  4. 1        198011
  5. 1        198012
  6. 1        198101
  7. 1        198102
  8. 1        198103
  9. 2        201101
  10. 2        201102
  11. 2        201103
  12. 2        201104
  13. ;

  14. data want(drop=startmonth);
  15.         set have;
  16.         by ID notsorted;
  17.         retain first last;
  18.         if first.ID then first=startmonth;
  19.         if last.ID then do;
  20.                 last=startmonth;
  21.                 dif=intck('month',first,last);
  22.                 output;
  23.         end;
  24.         format first last yymmn6.;
  25. run;
复制代码
我的博客: http://elek.me/sas
联系我: http://about.me/elek

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-3 00:14