楼主: liuliuqiu
1302 12

[问答] 求教SAS程序 [推广有奖]

  • 6关注
  • 2粉丝

副教授

62%

还不是VIP/贵宾

-

威望
0
论坛币
2340 个
通用积分
8.9561
学术水平
3 点
热心指数
5 点
信用等级
5 点
经验
14393 点
帖子
429
精华
0
在线时间
1113 小时
注册时间
2009-3-24
最后登录
2024-2-29

15论坛币
例如有两个数据集 a和b
a   id   startyear
    1    2005
    2    2003
    2    2005
    3    2002
    ....

b   id   country  startyear
     1     032       2004
     1     056       2006
     1     018       2003
     2     013       2001
     2     057      2003
     2     025      2005
     3     018      2003
     3     025      2004
     4     ....
如上两个数据集,都含有id,startyear代表开始进行某种行为的初始时间。
想得到的数据集:以数据集a为基础,个体1,开始行为时间为2005年,对应数据集b,找出数据集b中id有无在2005年前的数据,有则为1,且统计出个数,如b中个体1有两条在2005年前开始的记录(country为032和018)
want   id  startyear  ex  exnumber
          1    2005       1       2
          2    2003       1       1
          2    2005       1       2
          3    2002       0       0
          4    ....

关键词:sas程序 Country starty Number Start 程序
沙发
420948492 发表于 2016-8-3 10:05:52 |只看作者 |坛友微信交流群
试试双SET或者proc SQL非等值连接

使用道具

藤椅
liuliuqiu 发表于 2016-8-3 10:07:22 |只看作者 |坛友微信交流群
420948492 发表于 2016-8-3 10:05
试试双SET或者proc SQL非等值连接
能否劳烦您写出程序呢?不大会呢

使用道具

板凳
420948492 发表于 2016-8-3 10:08:23 |只看作者 |坛友微信交流群
liuliuqiu 发表于 2016-8-3 10:07
能否劳烦您写出程序呢?不大会呢
上传一份样例数据吧

使用道具

报纸
420948492 发表于 2016-8-3 10:23:56 |只看作者 |坛友微信交流群
  1. data a;
  2. input  id   startyear;
  3. cards;
  4. 1    2005
  5. 2    2003
  6. 2    2005
  7. 3    2002
  8. ;
  9. run;

  10. data b;
  11. input  id   country  startyear;
  12. cards;
  13. 1     032       2004
  14. 1     056       2006
  15. 1     018       2003
  16. 2     013       2001
  17. 2     057      2003
  18. 2     025      2005
  19. 3     018      2003
  20. 3     025      2004
  21. ;
  22. run;

  23. proc sql;
  24. create table w as
  25. select a.*,b.country,b.startyear as b_startyear from a left join b
  26. on a.id=b.id and a.startyear>b.startyear;
  27. quit;

  28. proc sql;
  29. create table wanted as
  30. select id,startyear,(count(country)>0) as ex,count(country) as exnumber from w
  31. group by id,startyear;
  32. quit;
复制代码

使用道具

地板
420948492 发表于 2016-8-3 10:25:51 |只看作者 |坛友微信交流群
数据量不是很大的话,PROC SQL的效率是没问题的,如果太大可以考虑使用双SET

使用道具

7
高贵林 发表于 2016-8-3 12:28:51 |只看作者 |坛友微信交流群
  1. data a;
  2. input  id   startyear;
  3. datalines;
  4.     1    2005
  5.     2    2003
  6.     2    2005
  7.     3    2002
  8. ;
  9. run;

  10. data b ;
  11. input  id   country  startyear;
  12. datalines;
  13.      1     032       2004
  14.      1     056       2006
  15.      1     018       2003
  16.      2     013       2001
  17.      2     057      2003
  18.      2     025      2005
  19.      3     018      2003
  20.      3     025      2004
  21. ;
  22. run;


  23. proc sql;
  24. select a.id, a.startyear, count(b.startyear)>0 as ex,  
  25.         count(b.startyear) as exnumber
  26.         from a left join b
  27. on a.id=b.id and a.startyear>b.startyear
  28. group by a.id, a.startyear;
  29. quit;
复制代码

使用道具

8
wwang111 发表于 2016-8-4 07:24:03 |只看作者 |坛友微信交流群
data a;
input  id   startyear;
cards;
1    2005
2    2003
2    2005
3    2002
;
run;

data b;
input  id   country $  startyear;
cards;
1     032       2004
1     056       2006
1     018       2003
2     013       2001
2     057      2003
2     025      2005
3     018      2003
3     025      2004
;


data test;
set a;
  ex=0;
  exnumber=0;
do i=1 to nobs;
set b(rename=(id=id1 startyear=startyear1)) nobs=nobs point=i;
if id=id1 and startyear>startyear1 then do;
   ex=1;
   exnumber+1;
end;
end;
drop startyear1 id1;
run;

使用道具

9
liuliuqiu 发表于 2016-8-4 11:14:45 |只看作者 |坛友微信交流群
a.xlsx (57.45 KB) b.xlsx (44.79 KB)

使用道具

10
liuliuqiu 发表于 2016-8-4 11:18:03 |只看作者 |坛友微信交流群
a和b中含有变量var country startyear。 var代表企业,country代表出口的国家,startyear表示企业对某国开始出口的时间。以a为基础,利用数据集b得到:企业在对某国出口之前,是否对其他国家进行过出口,并计算出口国家的个数。

使用道具

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

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

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

GMT+8, 2024-4-20 22:47