楼主: 3qsir
1102 7

[有偿编程] How to extract fixed rolling column(24) then rename those [推广有奖]

  • 2关注
  • 1粉丝

副教授

52%

还不是VIP/贵宾

-

威望
0
论坛币
13606 个
通用积分
3.0000
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
71839 点
帖子
368
精华
0
在线时间
931 小时
注册时间
2011-5-18
最后登录
2021-3-6

30论坛币
Sir : Can you do debug ? THANKS!
How to use one file(monthly.xlsx) to roll cloumn from 191101 to 201103 fix to 24 variables every moving windows, then rename variable(column) name =K1......K24
---------------------------
H201103 outcomes is only  one my outcome ,
I want to be produce many like H201103 files
that involve from H199101 H199102 ...  to H201103.
--------------------------------------------------------------
Monthly.xlsx
IND         199101  199102......     201301    201302
sz1_bm1  -3.45       8.05  .....        5.76         -1.64
sz1_bm2  -1            8.22......        5.77           -2.35
sz1_bm3 -1.93         8.47......         5.68         -1.99
sz1_bm4 -1.86         7.7   .......      6.87          -1.44
sz1_bm5 -3.15         8.36  .....        10.22      -2.61
sz2_bm1 -1.6           9.68  ....         4.5            1.2
sz2_bm2 -2.89          7.18 ....         5.84      -0.19
------------------------------------------------------
H201103 SAS outcomes:
IND             K1         K2   ...................      K23        K24
sz1_bm1     0.69       7.47 ...................      5.76       -1.64
sz1_bm2     1.35       7.32 ....................... 5.77       -2.35
sz1_bm3     0.81       7.07                         5.68        -1.99
sz1_bm4     2.02       6.44                         6.87        -1.44
sz1_bm5     0.8         6.42                        10.22        -2.61
sz2_bm1     1.35        7.53                       4.5             1.2
sz2_bm2      2.39       6.78                      5.84           -0.19
----------------------------------------------------------------------------
* thanks for nomad5's code*

%macro m(v);


%lety=%substr(&v.,1,4);


%letm=%substr(&v.,5,2);


%doi=1 %to 24;


    %let mf=%sysfunc(putn(%eval(&m.+&i.-1),z2.));


    %if &mf.>24 %then %do;


        %let mf=%sysfunc(putn(%eval(&mf.-24),z2.));


        %let yf=%eval(&y.+2);


    %end;


    %else %if &mf.>12 %then %do;


        %let mf=%sysfunc(putn(%eval(&mf.-12),z2.));


        %let yf=%eval(&y.+1);


    %end;


    %else %let yf=&y.;


     ,t1."&yf.&mf."n asK&i.


      %end;


%mend m;


/*%m(201103);*/


proc sql noprint;


    create table test as


   select t1.IND


       %m(201103)


    from work.monthly t1


    ;


quit;


关键词:extract rolling column rename extra rooling involved windows because example
沙发
yongyitian 发表于 2014-3-13 19:26:16 |只看作者 |坛友微信交流群
  1. %macro m(v);
  2. %let y=%substr(&v.,1,4);
  3. %let m=%substr(&v.,5,2);
  4. %do i=1 %to 24;
  5.       %let mf=%sysfunc(putn(%eval(&m.+&i.-1),z2.));
  6.       %if &mf.>24 %then %do;
  7.         %let mf=%sysfunc(putn(%eval(&mf.-24),z2.));
  8.         %let yf=%eval(&y.+2);
  9.     %end;
  10.     %else %if &mf.>12 %then %do;
  11.         %let mf=%sysfunc(putn(%eval(&mf.-12),z2.));
  12.         %let yf=%eval(&y.+1);
  13.     %end;
  14.     %else %let yf=&y.;
  15.       ,t1.%sysfunc(cats(%bquote('&yf.&mf.'), n)) as K&i.
  16.   %end;
  17. %mend m;

  18. %macro split;  /* this is new */
  19.    %do year = 1991 %to 2011;
  20.       %do month = 1 %to 12;
  21.             %let m2=%sysfunc(putn(&month, z2.));
  22.             %let yearmon=&year&m2;
  23.       %if &yearmon le 201103 %then %do;
  24.        proc sql noprint;
  25.          create table H&yearmon as
  26.          select t1.IND
  27.          %m(&yearmon)
  28.        from work.monthly t1
  29.        ;
  30.     quit; %end;

  31.      %end; %end;
  32. %mend split;
  33. %split
复制代码

使用道具

藤椅
yongyitian 发表于 2014-3-14 11:20:30 |只看作者 |坛友微信交流群
  1. /* A minor change was made on the code.

  2. But using data step is much easier and the code should be easy to read
  3. Since the variables names are numbers you can easily get an ordered list of  
  4. variables names as shown in my previous posts, you just need to rename the variables.  */

  5. %macro m(v);
  6. %let y=%substr(&v.,1,4);
  7. %let m=%substr(&v.,5,2);
  8. %do i=1 %to 24;
  9.       %let mf=%sysfunc(putn(%eval(&m.+&i.-1),z2.));
  10.       %if &mf.>24 %then %do;
  11.         %let mf=%sysfunc(putn(%eval(&mf.-24),z2.));
  12.         %let yf=%eval(&y.+2);
  13.     %end;
  14.     %else %if &mf.>12 %then %do;
  15.         %let mf=%sysfunc(putn(%eval(&mf.-12),z2.));
  16.         %let yf=%eval(&y.+1);
  17.     %end;
  18.     %else %let yf=&y.;
  19.       ,t1.%sysfunc(cats(%bquote('&yf.&mf.'), n)) as K&i.
  20.   %end;
  21. %mend m;

  22. /*%m(201103);*/
  23. proc sql noprint;
  24.     create table test as
  25.      select t1.IND
  26.      %m(201103)
  27.      from work.monthly t1
  28.     ;
  29. quit;
复制代码

LOG.JPG (59.02 KB)

LOG.JPG

LOG.JPG (59.02 KB)

LOG.JPG

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
3qsir + 5 + 5 + 5 观点有启发

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

使用道具

板凳
3qsir 发表于 2014-3-14 16:50:37 |只看作者 |坛友微信交流群
OK! Thank for your help.
You use the simply pro sql through %m(201103)

BUT : H201103 outcomes is only  one my outcome ,
I want to be produce many like H201103 files
that include from H199101 H199102 ...  to H201103.

I must excute 243 times of %macro : from %m(199101) to %m(201103)
So must  consture  the another macro in this section (proc sql):
THS!

/*%m(201103);*/
proc sql noprint;
     create table test as
     select t1.IND
        %m(201103)
    from work.monthly t1;
quit;

使用道具

报纸
intheangel 学生认证  发表于 2014-3-14 18:10:28 |只看作者 |坛友微信交流群
3qsir 发表于 2014-3-14 16:50
OK! Thank for your help.
You use the simply pro sql through %m(201103)
you can create a macro to repeat this code



%macro a;
%do i=199101 %to 201103;
%macro m(v);
%let y=%substr(&v.,1,4);
%let m=%substr(&v.,5,2);
%do i=1 %to 24;
      %let mf=%sysfunc(putn(%eval(&m.+&i.-1),z2.));
      %if &mf.>24 %then %do;
        %let mf=%sysfunc(putn(%eval(&mf.-24),z2.));
        %let yf=%eval(&y.+2);
    %end;
    %else %if &mf.>12 %then %do;
        %let mf=%sysfunc(putn(%eval(&mf.-12),z2.));
        %let yf=%eval(&y.+1);
    %end;
    %else %let yf=&y.;
      ,t1.%sysfunc(cats(%bquote('&yf.&mf.'), n)) as K&i.
  %end;
%mend m;

/*%m(201103);*/
proc sql noprint;
    create table test&i as
     select t1.IND
     %m(&i)
     from work.monthly t1
    ;
quit;
%end;
%mend a;
%a;
我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶

使用道具

地板
3qsir 发表于 2014-3-14 19:34:18 |只看作者 |坛友微信交流群
BUG !!!

使用道具

7
intheangel 学生认证  发表于 2014-3-14 20:28:41 |只看作者 |坛友微信交流群
%macro a;
%do mm=199101 %to 201103;
%macro m(v);
%let y=%substr(&v.,1,4);
%let m=%substr(&v.,5,2);
%do i=1 %to 24;
      %let mf=%sysfunc(putn(%eval(&m.+&i.-1),z2.));
      %if &mf.>24 %then %do;
        %let mf=%sysfunc(putn(%eval(&mf.-24),z2.));
        %let yf=%eval(&y.+2);
    %end;
    %else %if &mf.>12 %then %do;
        %let mf=%sysfunc(putn(%eval(&mf.-12),z2.));
        %let yf=%eval(&y.+1);
    %end;
    %else %let yf=&y.;
      ,t1.%sysfunc(cats(%bquote('&yf.&mf.'), n)) as K&i.
  %end;
%mend m;

/*%m(201103);*/
proc sql noprint;
    create table test&mm as
     select t1.IND
     %m(&mm)
     from work.monthly t1
    ;
quit;
%end;
%mend a;
%a;
我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶

使用道具

8
intheangel 学生认证  发表于 2014-3-14 20:29:28 |只看作者 |坛友微信交流群
the variable has been used ,i have not look carefully~
我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶

使用道具

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

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

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

GMT+8, 2024-4-27 23:22