楼主: liuliuqiu
4486 25

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

21
FB_FLORA 发表于 2014-6-27 16:35:28
liuliuqiu 发表于 2014-6-27 16:25
老师,实在不好意思,结果不一致可能是因为我上例中hs只含有一个值,如果说hs不止有一种值呢?例如
hs   ...
我也是刚在学sas,那hs是什么含义?需要根据hs的不同做什么处理吗?

22
liuliuqiu 发表于 2014-6-27 16:45:38
FB_FLORA 发表于 2014-6-27 16:35
我也是刚在学sas,那hs是什么含义?需要根据hs的不同做什么处理吗?
对,要根据hs的不同做处理。例如说:
hs    code    year1     year2  number
1     1234    2004    2004     0
1     2345    2004    2004     0
1     3456    2004    2004     0
1     3456    2005    2004     2
1     4567    2004    2004     0
1     4567    2005    2004     2
1     5678    2004    2004    0
1     5678    2005    2004    2
1     5678    2006    2004     4
2     2478    2005     2005   0
2     1234    2005    2005    0
2     1234    2006    2005    1
多谢了

23
hello_fj 发表于 2014-6-27 16:55:01
liuliuqiu 发表于 2014-6-27 16:45
对,要根据hs的不同做处理。例如说:
hs    code    year1     year2  number
1     1234    2004    2 ...
  1. data  test;
  2. input hs code year1 year2;
  3. cards;
  4. 1 1234 2004 2004
  5. 1 2345 2004 2004
  6. 1 3456 2004 2004
  7. 1 3456 2005 2004
  8. 1 4567 2004 2004
  9. 1 4567 2005 2004
  10. 1 5678 2004 2004
  11. 1 5678 2005 2004
  12. 1 5678 2006 2004
  13. 2 1234 2004 2004
  14. 2 2345 2004 2004
  15. 2 3456 2004 2004
  16. 2 3456 2005 2004
  17. 2 4567 2004 2004
  18. 2 4567 2005 2004
  19. 2 5678 2004 2004
  20. 2 5678 2005 2004
  21. 2 5678 2006 2004
  22. ;
  23. run;


  24. proc sort data=test;
  25. by hs code;
  26. run;

  27. data test1;
  28.   set test;
  29.   by hs code;
  30.   if first.hs then n=0;
  31.   if first.code then n+1;
  32.   else n+0;
  33. run;

  34. proc sort data=test1;
  35. by hs year1 code;
  36. run;

  37. data want;
  38. set test1;
  39. by hs year1;
  40. if first.year1 then do;
  41.   retain number;
  42.   if year1=year2 then number=0;
  43.   else number=n-1;
  44. end;
  45. drop n;
  46. run;

  47. proc sort data=want;
  48. by hs code year1;
  49. run;
复制代码
看看是不是你想要的
公众号:SAS与量化投资

24
hello_fj 发表于 2014-6-27 16:57:56
liuliuqiu 发表于 2014-6-27 16:23
老师,实在不好意思,我再请教一个问题,如果说hs不止有一种值呢?例如
hs    code    year1   year2    ...
从你的表述来看 只需要对不同的产品进行计算而已 你看看程序中的例子

25
FB_FLORA 发表于 2014-6-27 16:59:59
liuliuqiu 发表于 2014-6-27 16:45
对,要根据hs的不同做处理。例如说:
hs    code    year1     year2  number
1     1234    2004    2 ...
你试试这样行吗?
data sample;
input hs code year1 year2;
unicode=strip(hs)||"-"||strip(code);
datalines;
1 1234 2004 2004
1 2345 2004 2004
1 3456 2004 2004
1 3456 2005 2004
1 4567 2004 2004
1 4567 2005 2004
1 5678 2004 2004
1 5678 2005 2004
1 5678 2006 2004
2 2478 2005 2005
2 1234 2005 2005
2 1234 2006 2005
;

proc sql;
create table sam1 as
select  hs , code , max(year1) as year
from sample
group by hs , code
order by hs , code
;
create table sam2 as
select hs , year, count(distinct code) as num
from sam1
group by hs , year
order by hs , year
;
quit;
data sam21;
set sam2;
by hs year;
order=_n_;
lorder=_n_+1;
run;
data sam22;
merge sam21(in=a drop=lorder) sam21(drop=order year rename=(lorder=order num=lastnum));
by hs order;
retain cumnum;
if a;
if missing(lastnum) then lastnum=0;
if first.hs then cumnum=0;
cumnum+lastnum;
run;
proc sql;
create table sample1 as
select *
from sample a , sam22 b
where a.year1=b.year and a.hs=b.hs
;
quit;
data sample2;
set sample1;
if year1=year2 then number=0;
else if year1>year2 then number=cumnum;
run;
proc sort data=sample2;
by hs code;
run;
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
liuliuqiu + 2 + 2 + 2 热心帮助其他会员

总评分: 学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

26
liuliuqiu 发表于 2014-6-27 17:46:49
FB_FLORA 发表于 2014-6-27 16:59
你试试这样行吗?
data sample;
input hs code year1 year2;
感谢感谢,已经成功做出来了,多谢了

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

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