Question

I used YYQ5. to create a new variable based on dates in MMDDYY10. format. The problem is is that I have the date range for my annual quarters to be different than the SAS annual quarter period.

I originally had something like:

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;

EDIT: updated date labels.

Thanks.

Was it helpful?

Solution

Yep, you can use INTCK() function with shifted interval:

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;

OTHER TIPS

I would define a function in FCMP to calculate your quarter value. Then create a new format based on that function (or just use the function in the data step).

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;

Produces:

                          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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top