楼主: yingxin0824
1752 4

SAS程序请帮忙 [推广有奖]

  • 1关注
  • 0粉丝

硕士生

21%

还不是VIP/贵宾

-

威望
0
论坛币
29630 个
通用积分
1.3500
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
426 点
帖子
37
精华
0
在线时间
229 小时
注册时间
2012-2-13
最后登录
2024-4-25

20论坛币
我的问题如下:

对于第1条记录希望生成t1=2015/01/01加上1个月得到2015/02/01, t2=加上2个月得到2015/03/01,以此类推生成t3,t4
对于第2条记录则希望生成t1,t2,t3,t4,t5,生成的变量数量依赖于months所对应的值。
对于第3条记录则希望生成t1,t2,t3,t4,t5,t6



                time         months      
1         2015/01/01       4
2         2015/02/04       5
3         2015/02/07       6
……          ……              ……

应该怎样编制程序呢?

关键词:sas程序 months month mont time SAS 程序 相加
  1. data a;
  2. input time yymmdd10. months;
  3. format time yymmdd10.;
  4. cards;
  5. 2015/01/01 4
  6. 2015/02/04 5
  7. 2015/02/07 6
  8. ;
  9. run;
  10. proc sql noprint;
  11.         select count(*) into:nobs from a;
  12. quit;
  13. %macro test;

  14. %do obs=1 %to &nobs;
  15. data a;
  16.         set a;
  17.         if _n_=&obs then call symputx('tmax',months);
  18. run;
  19. %put &tmax;

  20. data a;
  21.         set a;
  22.         if _n_=&obs then do;
  23.                 %do i=1 %to &tmax;
  24.                 format t&i yymmdd10.;
  25.                 t&i=intnx('month',time,1,'sameday');
  26.                 %end;
  27.         end;
  28. run;
  29. %end;
  30. %mend;
  31. %test
复制代码

使用道具

藤椅
CTR1013 发表于 2016-3-3 17:17:12 |只看作者 |坛友微信交流群
我抛砖引玉,用个笨办法:
data a;
        id=1;time=mdy(1,1,2015);months=4;output;
        id=2;time=mdy(2,4,2015);months=5;output;
        id=3;time=mdy(7,7,2015);months=6;output;
        format time yymmdd10.;
run;

data b;
        set a;
        do i=1 to months;
                if month(time)+i<=12 then time1=mdy(month(time)+i,day(time),year(time));
                else time1=mdy(month(time)+i-12,day(time),year(time)+1);
                output;
        end;
        format time1 yymmdd10.;
run;
proc sort data=b;
        by id time months;
run;
proc transpose data=b out=c prefix=t;
        by id time months;
        var time1;
run;

使用道具

板凳
dogmamongo 发表于 2016-3-3 20:47:44 |只看作者 |坛友微信交流群
假设你的档案命名为a
且month 以及是日期格式
就用下面这个方法
array也许可以 但是我不熟

%macro a;
data a;
   set a;
   %do i=1 %to 100;
       t&i=INTNX('MONTH',time,&i,'same');
         format t&i yymmddn8.;
       if &i>month then t&i=.;
   %end;
run;
%mend;
%a;

使用道具

报纸
freerunning_sky 在职认证  发表于 2016-3-4 10:20:58 |只看作者 |坛友微信交流群
  1. data input;
  2.         input time months;
  3.         informat time yymmdd10.;
  4.         format time yymmdd10.;
  5.         cards;
  6.         2015/01/01 4
  7.         2015/02/04 5
  8.         2015/02/07 6
  9.         ;
  10. run;

  11. proc sql noprint;
  12.         select cat(max(months)) into :max_num from input;
  13. quit;

  14. data output;
  15.         set input;
  16.         array t(*) t1-t&max_num;
  17.         format t1-t&max_num yymmdd10.;
  18.         do i=1 to months;
  19.                 t(i)=intnx('month',time,i);
  20.         end;
  21.         drop i;
  22. run;
复制代码

使用道具

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

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

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

GMT+8, 2024-5-5 11:51