楼主: icmars88
3459 7

[问答] sas 多级 分类 循环 自定义 函数 [推广有奖]

  • 0关注
  • 0粉丝

小学生

64%

还不是VIP/贵宾

-

威望
0
论坛币
2006 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
192 点
帖子
5
精华
0
在线时间
11 小时
注册时间
2014-5-23
最后登录
2015-11-10

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
各位大神好,遇到了个问题,研究了1天都没有实现出来,实在没有办法,只能求助各位大神。
问题如下:
可以概括为按照多层级分类(A到B到C),C是时间序列,并对C的值求环比和同比。
data sample:
data sample;
input com$  cou$  cate$ Year_Month  sum best10.2;
cards;
3301260251        阿尔巴尼亚        W        201310        535
3301260262        阿尔巴尼亚        E        201311        8132
33189619KK        阿尔巴尼亚        A        201404        5630.4
33189619MF        阿尔巴尼亚        G        201405        4150.6
33189619PN        阿尔巴尼亚        F        201404        11233.2
33189619R1        阿尔巴尼亚        C        201303        1216
33189619RC        阿尔巴尼亚        B        201309        6902
33189619S9        阿尔巴尼亚        A        201311        3960
33189619SL        阿尔巴尼亚        B        201409        921.6
33189619SL        阿尔巴尼亚        B        201410        2802.3
33189619SL        阿尔巴尼亚        A        201412        22263.5
33189619SQ        阿尔巴尼亚        G        201303        13950
33189619YR        阿尔巴尼亚        V        201409        4465.81
33189619Z6        阿尔巴尼亚        A        201412        1410
3318961A0C        阿尔巴尼亚        A        201412        9909.74
3318961A1J        阿尔巴尼亚        B        201406        4500
3318961A1J        阿尔巴尼亚        A        201407        11610
3318961A1J        阿尔巴尼亚        B        201412        1128.6

run;

想要的效果是:
先历遍cate,每一个cate中再历遍其对应的cou,每一个cou再历遍com,然后读取每个com中的时间序列对应的值,计算每个com的每个月的同比和环比。

类似结果
catecoucomtimesumMoMYoY
A阿尔巴尼亚33189619MF

201301

3231.013621

..
A阿尔巴尼亚33189619MF

201302

2142.708878

-33.68%

.
A阿尔巴尼亚33189619MF

201303

2265.315223

5.72%

.
A阿尔巴尼亚33189619MF

201304

2883.945439

27.31%

.
A阿尔巴尼亚33189619MF

201305

3247.497452

12.61%

.
A阿尔巴尼亚33189619MF

201306

2949.295797

-9.18%

.
A阿尔巴尼亚33189619MF

201307

3155.988678

7.01%

.
A阿尔巴尼亚33189619MF

201308

3560.774

12.83%

.
A阿尔巴尼亚33189619MF

201309

3635.2451

2.09%

.
A阿尔巴尼亚33189619MF

201310

3124.634714

-14.05%

.
A阿尔巴尼亚33189619MF

201311

3008.229843

-3.73%

.
A阿尔巴尼亚33189619MF

201312

3341.739672

11.09%

.
A阿尔巴尼亚33189619MF

201401

3804.965298

13.86%

17.76%

A阿尔巴尼亚33189619MF

201402

1094.977975

-71.22%

-48.90%

A阿尔巴尼亚33189619MF

201403

2754.556122

151.60%

21.60%

A阿尔巴尼亚33189619MF

201404

3440.162338

24.89%

19.29%

A阿尔巴尼亚33189619MF

201405

3293.713571

-4.26%

1.42%

A阿尔巴尼亚33189619MF

201406

3911.709254

18.76%

32.63%

A阿尔巴尼亚33189619MF

201407

4691.571004

19.94%

48.66%

A阿尔巴尼亚33189619MF

201408

6160.725138

31.31%

73.02%

A阿尔巴尼亚33189619MF

201409

6332.936877

2.80%

74.21%

A阿尔巴尼亚33189619MF

201410

4774.602979

-24.61%

52.81%

A阿尔巴尼亚33189619MF

201411

5001.048961

4.74%

66.25%

A阿尔巴尼亚33189619MF

201412

5783.011391

15.64%

73.05%

A阿尔巴尼亚33189619MF

201501

6309.410916

9.10%

65.82%

A阿尔巴尼亚33189619MF

201502

4925.020461

-21.94%

349.80%

A阿尔巴尼亚33189619MF

201503

2935.954366

-40.39%

6.59%

A阿尔巴尼亚33189619MF

201504

4828.603033

64.46%

40.36%

A阿尔巴尼亚33189619MF

201506

5867.749124

21.52%

78.15%



跪求指导,谢谢

二维码

扫码加我 拉你入群

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

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

关键词:自定义 Sample 阿尔巴尼亚 Times Input 阿尔巴尼亚 天都

沙发
yangdelong1988 发表于 2015-8-25 00:35:19 |只看作者 |坛友微信交流群
1、如果时间数据都像你上面列的那样没有缺失,那就可以先对数据排序,然后用lag函数,环比lag1,同步lag12;2、如果时间有缺失,那建议先根据时间找出其对应的同比、环比时间,然后用proc sql步通过cate、cou、com和同环比时间关联sum值。

使用道具

藤椅
icmars88 发表于 2015-8-25 09:10:04 |只看作者 |坛友微信交流群
yangdelong1988 发表于 2015-8-25 00:35
1、如果时间数据都像你上面列的那样没有缺失,那就可以先对数据排序,然后用lag函数,环比lag1,同步lag12; ...
昨晚又试了一下,我现在就是不知道怎么写,有两个主要的问题,
1、不知道如何找出对应的时间的值;
2、不知道怎么用多层循环出想要结果,如果用proc sql,也可以分类,但是就是第一个问题不知道怎么定位。

望能再详细点。谢谢,或者能够个两层的demo参考一下。

使用道具

板凳
yangdelong1988 发表于 2015-8-25 09:26:43 |只看作者 |坛友微信交流群
  1. data sample;
  2. input com:$15. cou:$15. cate:$2. year_month:$6. sum;
  3. year=substr(year_month,1,4);
  4. month=substr(year_month,5,2);
  5. year_month_1=mdy(month,'01',year);*通过substr函数提取日期,新日期为year_month的第一天;
  6. format year_month_1 yymmdd10.;
  7. year_month_hb=intnx('month',year_month_1,-1);*通过intnx函数计算出当前日期前1月、前12月对应日期;
  8. year_month_tb=intnx('month',year_month_1,-12);
  9. format year_month_hb yymmdd10.;
  10. format year_month_tb yymmdd10.;
  11. drop year month;
  12. cards;
  13. 3301260251 阿尔巴尼亚 W 201310 535
  14. ;
  15. run;
复制代码

你的日期是年和月的,将它变为年月日的,为当月的第一天,如201506就变为2015-06-01,然后计算其环比、同比时间,再通过proc sql关联

使用道具

报纸
icmars88 发表于 2015-8-25 12:45:21 |只看作者 |坛友微信交流群
yangdelong1988 发表于 2015-8-25 09:26
你的日期是年和月的,将它变为年月日的,为当月的第一天,如201506就变为2015-06-01,然后计算其环比、同 ...
谢谢你,我大概弄出来了,但是有个新问题……
11111.png

代码如下:
data EX_YM_SUM_test;
        set EX_YM_SUM;
        format per_price best4.2
        MoM_amount percentn8.2
        MoM_weight percentn8.2
        MoM_per percentn8.2
        YoY_amount percentn8.2
        YoY_weight percentn8.2
        YoY_per percentn8.2;
        per_price = EX_YM_sum_price/EX_YM_sum_weight;
        lag1_date=intnx('month',date_01,-1);
        lag12_date=intnx('month',date_01,-12);
        format lag1_date yymmdd10.;
        format lag12_date yymmdd10.;
        if lag(trim(hs_code_8))=trim(hs_code_8) and lag(date_01)=lag1_date then do;
                MoM_amount = dif(EX_YM_sum_price)/lag(EX_YM_sum_price);
                MoM_weight = dif(EX_YM_sum_weight)/lag(EX_YM_sum_weight);
                MoM_per = dif(per_price)/lag(per_price);
        end;
        if lag12(trim(hs_code_8))=trim(hs_code_8) and lag12(date_01)=lag12_date then do;
                YoY_amount = dif12(EX_YM_sum_price)/lag12(EX_YM_sum_price);
                YoY_weight = dif12(EX_YM_sum_weight)/lag12(EX_YM_sum_weight);
                YoY_per = dif12(per_price)/lag12(per_price);
        end;
        drop lag1_date lag12_date;
        drop EX_YM_sum_price EX_YM_sum_weight per_price;
run;


问题,为什么第一个73239300的2014年的同比没有了,明明判断为真的,但是没有数据。

使用道具

地板
yangdelong1988 发表于 2015-8-25 15:07:18 |只看作者 |坛友微信交流群
我不建议你dif和lag函数来做,你那个数据时间不是完全连续的,可能缺少某个月的值,你先求出对应的同环比时间,然后再从原数据集(备份一个数据集)中关联出对应的同环比时间的sum值,能关联出来的就有同环比,不能关联出来的就没有,然后计算同环比增幅

使用道具

7
icmars88 发表于 2015-8-25 16:59:55 |只看作者 |坛友微信交流群
yangdelong1988 发表于 2015-8-25 15:07
我不建议你dif和lag函数来做,你那个数据时间不是完全连续的,可能缺少某个月的值,你先求出对应的同环比时 ...
非常感谢你的指点!!!
已经做出来了,再次感谢!!!
基本思路应该是,首先建立一个各个层级的变量组合后无重复的一个表,然后把各个层级的变量按需求用cat把变量和时间组合创建一个新的key,然后用sql自相连出来。

使用道具

8
忍冬的秋 发表于 2015-8-25 17:27:49 |只看作者 |坛友微信交流群
我把 原始 表中的33189619RC 改成33189619SL了
不然 木有 同比。


上code
data a;
input com : $20. cou : $20. cate $ year_month : $6. sum;
cards;
3301260251        阿尔巴尼亚        W        201310        535
3301260262        阿尔巴尼亚        E        201311        8132
33189619KK        阿尔巴尼亚        A        201404        5630.4
33189619MF        阿尔巴尼亚        G        201405        4150.6
33189619PN        阿尔巴尼亚        F        201404        11233.2
33189619R1        阿尔巴尼亚        C        201303        1216
33189619SL        阿尔巴尼亚        B        201309        6902
33189619S9        阿尔巴尼亚        A        201311        3960
33189619SL        阿尔巴尼亚        B        201409        921.6
33189619SL        阿尔巴尼亚        B        201410        2802.3
33189619SL        阿尔巴尼亚        A        201412        22263.5
33189619SQ        阿尔巴尼亚        G        201303        13950
33189619YR        阿尔巴尼亚        V        201409        4465.81
33189619Z6        阿尔巴尼亚        A        201412        1410
3318961A0C        阿尔巴尼亚        A        201412        9909.74
3318961A1J        阿尔巴尼亚        B        201406        4500
3318961A1J        阿尔巴尼亚        A        201407        11610
3318961A1J        阿尔巴尼亚        B        201412        1128.6
;
run;
data b;
set a;
year=substr(year_month,1,4);
month=substr(year_month,5,2);
run;
/*计算环比*/
proc sort data=b out=c;
by cate cou com year_month;
run;
data huanbi_final(drop = lag_yearmonth lag_sum temp year month);
set c;
by cate cou com;
retain lag_yearmonth 0 lag_sum 0;
temp=input(year_month,8.);
if first.com then do;
huanbi=0;
lag_yearmonth=0;
lag_sum=0;
end;
else if lag_yearmonth=temp-1 then
huanbi=(sum-lag_sum)/lag_sum;
output;
lag_yearmonth=temp;
lag_sum=sum;
run;
/*计算同比*/
proc sort data=b out=c_final;
by cate cou com month year;
run;
data tongbi_final(drop= lag_year lag_sum temp year month);
set c_final;
by cate cou com month;
retain lag_year 0 lag_sum 0;
temp=input(year,4.);
if first.month then do;
tongbi=0;
lag_year=0;
lag_sum=0;
end;
else if lag_year=temp-1 then
tongbi=(sum-lag_sum)/lag_sum;
output;
lag_year=temp;
lag_sum=sum;
run;
proc sort data=tongbi_final out=tongbi;
by cate cou com year_month sum;
run;
proc sort data=huanbi_final out=huanbi;
by cate cou com year_month sum;
run;
data union;
merge huanbi tongbi;
by cate cou com year_month sum;
run;

使用道具

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

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

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

GMT+8, 2024-5-11 08:14