全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
3800 7
2015-08-24
各位大神好,遇到了个问题,研究了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%



跪求指导,谢谢

二维码

扫码加我 拉你入群

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

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

全部回复
2015-8-25 00:35:19
1、如果时间数据都像你上面列的那样没有缺失,那就可以先对数据排序,然后用lag函数,环比lag1,同步lag12;2、如果时间有缺失,那建议先根据时间找出其对应的同比、环比时间,然后用proc sql步通过cate、cou、com和同环比时间关联sum值。
二维码

扫码加我 拉你入群

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

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

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

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

扫码加我 拉你入群

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

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

2015-8-25 09:26:43
复制代码

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

扫码加我 拉你入群

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

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

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年的同比没有了,明明判断为真的,但是没有数据。
二维码

扫码加我 拉你入群

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

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

2015-8-25 15:07:18
我不建议你dif和lag函数来做,你那个数据时间不是完全连续的,可能缺少某个月的值,你先求出对应的同环比时间,然后再从原数据集(备份一个数据集)中关联出对应的同环比时间的sum值,能关联出来的就有同环比,不能关联出来的就没有,然后计算同环比增幅
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群