楼主: liuliuqiu
1705 12

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

  • 6关注
  • 2粉丝

已卖:35份资源

副教授

64%

还不是VIP/贵宾

-

威望
0
论坛币
2239 个
通用积分
8.1161
学术水平
3 点
热心指数
5 点
信用等级
5 点
经验
14393 点
帖子
429
精华
0
在线时间
1129 小时
注册时间
2009-3-24
最后登录
2025-12-11

楼主
liuliuqiu 发表于 2016-8-3 09:47:12 |AI写论文
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
拉您进交流群
GMT+8, 2025-12-21 23:45