自定义季度月份范围

时间:2014-05-09 16:09:38

标签: sas

我使用YYQ5.根据MMDDYY10.格式的日期创建新变量。问题是我的年度季度的日期范围与SAS年度季度不同。

我最初有类似的东西:

data recog_flag;
    set recog;
    if '01nov2010'd <= FINAL_DECISION <= '31jan2011'd then quart = 1;
    else if '01feb2011'd <= FINAL_DECISION <= '30apr2011'd  then quart = 2;
    else if '01may2011'd <= FINAL_DECISION <= '31jun2011'd  then quart = 3;
    else if '01aug2011'd <= FINAL_DECISION <= '31oct2011'd  then quart = 4;
    else if '01nov2011'd <= FINAL_DECISION <= '31jan2012'd  then quart = 5;
    else if '01feb2012'd <= FINAL_DECISION <= '30apr2012'd  then quart = 6;
    run;

编辑:更新日期标签。

感谢。

2 个答案:

答案 0 :(得分:3)

是的,您可以使用移位间隔的INTCK()函数:

data have;
    do date='01Nov2010'd to '30Apr2012'd;
        output;
    end;
    format date MMDDYY10.;
run;

data want;
    set have;
    quart=intck('qtr.2','01Nov2010'd,date)+1;
run;

答案 1 :(得分:1)

我会在FCMP中定义一个函数来计算你的季度值。然后基于该函数创建一个新格式(或者只使用数据步骤中的函数)。

options cmplib=work.fns;

proc fcmp outlib=work.fns.formats;
function newQtr(date) $;
    new_date = intnx('month',date,2,'sameday');
    qtr = qtr(new_date);
    year = year(new_date);
    qtr = (year-2011)*4+qtr;
    return(put(qtr,4.));
endsub;
run;

proc format;
value nQTR
    other=[newQtr()];
run;

data test;
format x  date9. q nQTR. q2;
do x="01NOV2010"d to "31JAN2012"d by 25;
q=x;
q2 = input(newQTR(x),best.);
output;
end;
run;

proc print data=test(obs=10);
run;

产地:

                          Obs            x    q    q2

                             1    01NOV2010    1     1
                             2    26NOV2010    1     1
                             3    21DEC2010    1     1
                             4    15JAN2011    1     1
                             5    09FEB2011    2     2
                             6    06MAR2011    2     2
                             7    31MAR2011    2     2
                             8    25APR2011    2     2
                             9    20MAY2011    3     3
                            10    14JUN2011    3     3