楼主: liuliuqiu
1864 8

[有偿编程] SAS程序求教 [推广有奖]

  • 6关注
  • 2粉丝

已卖:35份资源

副教授

64%

还不是VIP/贵宾

-

威望
0
论坛币
2239 个
通用积分
8.1161
学术水平
3 点
热心指数
5 点
信用等级
5 点
经验
14393 点
帖子
429
精华
0
在线时间
1129 小时
注册时间
2009-3-24
最后登录
2025-12-11

楼主
liuliuqiu 发表于 2016-4-18 11:46:20 |AI写论文
5论坛币
现有数据
id        year
1          2001
1          2003
1         2005
2         2002
2         2004
2         2007
3         ...
上述数据的时间是不连续的,用SAS如何补充数据,让时间变量连续,例如个体1,现有时间的最大最小值为2005和2001,如何增加时间为2002和2004的数据,使时间变量连续?
感谢感谢!!
恳请给出具体程序,谢谢

最佳答案

孤单的我们 查看完整内容

写的很乱,应该有好的方法
关键词:sas程序 时间变量 year 具体程序 ear 如何

沙发
孤单的我们 发表于 2016-4-18 11:46:21
写的很乱,应该有好的方法
  1. data test;
  2. input id year;
  3. cards;
  4. 1 2001
  5. 1 2003
  6. 1 2005
  7. 2 2002
  8. 2 2004
  9. 2 2007
  10. ;
  11. run;


  12. data test1;
  13.         set test;
  14.         by id year;
  15.         dif=dif(year);

  16.         output;
  17.         if dif>1 then do;
  18.                 do i=dif to 2 by -1;
  19.                         year1=year-i+1;;output;
  20.                 end;
  21.         end;
  22. run;

  23. data test2;
  24.         set test1;
  25.         if year1>. then year=year1;
  26.         keep id year;
  27. run;

  28. proc sort data=test2;by id year;run;
复制代码

藤椅
liuliuqiu 发表于 2016-4-18 13:16:11
顶,请大神帮忙

板凳
孤单的我们 发表于 2016-4-18 13:28:27
liuliuqiu 发表于 2016-4-18 13:16
顶,请大神帮忙
回复需要通过审核。今天总是酱紫

报纸
liuliuqiu 发表于 2016-4-18 13:47:01
孤单的我们 发表于 2016-4-18 13:28
回复需要通过审核。今天总是酱紫
我等,非常感谢

地板
casper2 发表于 2016-4-18 13:59:49
options mprint symbolgen;

data test;
    input id year;
cards;
1 2001
1 2003
1 2005
2 2002
2 2004
2 2007
;
run;

proc sort data=test out=test;
    by id year;
run;

proc print; run;

proc sql;
    create table test1 as
    select id, min(year) as ymin, max(year) as ymax from test
    group by id
    order by id;
quit;

data test2;
    set test1;
    year = ymin;
    do while(year ge ymin and year le ymax);
         output;
         year + 1;
    end;
run;

proc print; run;

output:

                                          The SAS System          22:58 Sunday, April 17, 2016   1

                                        Obs    id    year

                                         1      1    2001
                                         2      1    2003
                                         3      1    2005
                                         4      2    2002
                                         5      2    2004
                                         6      2    2007
                                          The SAS System          22:58 Sunday, April 17, 2016   2

                                Obs    id    ymin    ymax    year

                                  1     1    2001    2005    2001
                                  2     1    2001    2005    2002
                                  3     1    2001    2005    2003
                                  4     1    2001    2005    2004
                                  5     1    2001    2005    2005
                                  6     2    2002    2007    2002
                                  7     2    2002    2007    2003
                                  8     2    2002    2007    2004
                                  9     2    2002    2007    2005
                                 10     2    2002    2007    2006
                                 11     2    2002    2007    2007


已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
liuliuqiu + 5 + 5 + 5 热心帮助其他会员
孤单的我们 + 5 + 1 + 1 + 1 do while:good idea!

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

7
casper2 发表于 2016-4-18 14:26:26 来自手机
给我来个email:omnitiger@gmail.com
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
liuliuqiu + 5 + 5 + 5 热心帮助其他会员

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

8
孤单的我们 发表于 2016-4-18 14:34:56
  1. data test;
  2. input id year;
  3. cards;
  4. 1 2001
  5. 1 2003
  6. 1 2005
  7. 2 2002
  8. 2 2004
  9. 2 2007
  10. ;
  11. run;


  12. data test1;
  13.         set test;
  14.         by id year;
  15.         dif=dif(year);
  16.         output;
  17.         if dif>1 then do;
  18.                 do i=dif to 2 by -1;
  19.                         year1=year-i+1;;output;
  20.                 end;
  21.         end;
  22. run;

  23. data test2;
  24.         set test1;
  25.         if year1>. then year=year1;
  26.         keep id year;
  27. run;

  28. proc sort data=test2;by id year;run;
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
liuliuqiu + 5 + 5 + 5 热心帮助其他会员

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

9
孤单的我们 发表于 2016-4-18 15:01:52
或者用宏

data test;
input id year;
cards;
1 2001
1 2003
1 2005
2 2002
2 2004
2 2007
;
run;

proc sql noprint;
        create table sort as
        select distinct id,max(year) as max,min(year) as min from test group by id;
quit;

data _null_;
        set sort;
        call symputx('nobs',_n_);
        call symputx('id'||strip(put(_n_,best.)),_n_);
        call symputx('max'||strip(put(_n_,best.)),max);
        call symputx('min'||strip(put(_n_,best.)),min);
run;
%macro test;
data want;
        retain id;
        %do i=1 %to &nobs;
                id=&&id&i;
                %do j=&&min&i %to &&max&i;
                year=&j;output;
        %end;
        %end;
run;
%mend;
%test;

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

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