我想创建一个宏来循环遍历年,季度,月和列值

时间:2016-07-20 18:56:50

标签: sas sas-macro

SAS问题:我有一个表有一列名为:Primary_coverage有4个观察,如下所示,

Primary_coverage 合并单一限额 没有保险的驾车人士 医 身体受伤

我想创建一个宏来循环遍历Year,Quarter,Month和column值,以便我的表值看起来如下所示:(2000Q1到2002Q4 = 36个月,12个季度,3年) 注意:最早的年份总是从2000Q1开始

year    quarter Primary_coverage    months
2000    1   combined single limit   36
2000    2   combined single limit   33
2000    3   combined single limit   30
2000    4   combined single limit   27
2001    1   combined single limit   24
2001    2   combined single limit   21
2001    3   combined single limit   18
2001    4   combined single limit   15
2002    1   combined single limit   12
2002    2   combined single limit   9
2002    3   combined single limit   6
2002    4   combined single limit   3
2000    1   uninsured motorist  36
2000    2   uninsured motorist  33
2000    3   uninsured motorist  30
2000    4   uninsured motorist  27
2001    1   uninsured motorist  24
2001    2   uninsured motorist  21
2001    3   uninsured motorist  18
2001    4   uninsured motorist  15
2002    1   uninsured motorist  12
2002    2   uninsured motorist  9
2002    3   uninsured motorist  6
2002    4   uninsured motorist  3
2000    1   Medical 36
2000    2   Medical 33
2000    3   Medical 30
2000    4   Medical 27
2001    1   Medical 24
2001    2   Medical 21
2001    3   Medical 18
2001    4   Medical 15
2002    1   Medical 12
2002    2   Medical 9
2002    3   Medical 6
2002    4   Medical 3
2000    1   Bodily Injury   36
2000    2   Bodily Injury   33
2000    3   Bodily Injury   30
2000    4   Bodily Injury   27
2001    1   Bodily Injury   24
2001    2   Bodily Injury   21
2001    3   Bodily Injury   18
2001    4   Bodily Injury   15
2002    1   Bodily Injury   12
2002    2   Bodily Injury   9
2002    3   Bodily Injury   6
2002    4   Bodily Injury   3

但是当我在2003Q2上运行相同的程序时我的表应如下所示:(2000Q1至2003Q2 = 42个月,14个季度,3年)

year    quarter Primary_coverage    months
2000    1   combined single limit   42
2000    2   combined single limit   39
2000    3   combined single limit   36
2000    4   combined single limit   33
2001    1   combined single limit   30
2001    2   combined single limit   27
2001    3   combined single limit   24
2001    4   combined single limit   21
2002    1   combined single limit   18
2002    2   combined single limit   15
2002    3   combined single limit   12
2002    4   combined single limit   9
2003    1   combined single limit   6
2003    2   combined single limit   3
2000    1   uninsured motorist  42
2000    2   uninsured motorist  39
2000    3   uninsured motorist  36
2000    4   uninsured motorist  33
2001    1   uninsured motorist  30
2001    2   uninsured motorist  27
2001    3   uninsured motorist  24
2001    4   uninsured motorist  21
2002    1   uninsured motorist  18
2002    2   uninsured motorist  15
2002    3   uninsured motorist  12
2002    4   uninsured motorist  9
2003    1   uninsured motorist  6
2003    2   uninsured motorist  3
2000    1   Medical 42
2000    2   Medical 39
2000    3   Medical 36
2000    4   Medical 33
2001    1   Medical 30
2001    2   Medical 27
2001    3   Medical 24
2001    4   Medical 21
2002    1   Medical 18
2002    2   Medical 15
2002    3   Medical 12
2002    4   Medical 9
2003    1   Medical 6
2003    2   Medical 3
2000    1   Bodily Injury   42
2000    2   Bodily Injury   39
2000    3   Bodily Injury   36
2000    4   Bodily Injury   33
2001    1   Bodily Injury   30
2001    2   Bodily Injury   27
2001    3   Bodily Injury   24
2001    4   Bodily Injury   21
2002    1   Bodily Injury   18
2002    2   Bodily Injury   15
2002    3   Bodily Injury   12
2002    4   Bodily Injury   9
2003    1   Bodily Injury   6
2003    2   Bodily Injury   3

1 个答案:

答案 0 :(得分:0)

使用日期函数的组合来实现此目的。使用INTNX递增/递减日期,使用input()将YYYYQQ转换为SAS日期。

循环是否直截了当。

%let end_date = 2003Q2;

data date_list;
    format end_date date9. coverage $20.;
    end_date=input("&end_date", yyq6.);
    n_months=intck('month', '01Jan2000'd, end_date)+3;



    do coverage='Primary', 'Secondary', 'Tertiary';
    date=end_date;
        do months=n_months to 3 by -3;
            year=year(date);
            quarter=qtr(date);
            date=intnx('month', date, -3, 'b');
            output;
        end;
    end;
    Keep year quarter months coverage;
run;