SMW
SMW

Reputation: 502

SAS Date Functions: Quarterly Equivelant of Day Function

I often use the day function in order to control date parameters within queries.

With the data step below I can call the beginning of the current month &bom based on whether or not the day of the month is le to the 8th of each month (in which that case we want &bom set to the 1st of the previous month), otherwise set &bom to the 1st of the current month.

data _null_;
call symput('current'," '" || put(intnx('day',today(),0),yymmdd10.) || "'");
call symput('bom'," '" || put(intnx('month',today(),0,'b'),yymmdd10.) || "'");
call symput('end'," '" || put(intnx('day',today(),-8,'e'),yymmdd10.) || "'");
if day(today()) le 8 then do;
call symput('bom'," '" || put(intnx('month',today(),-1,'b'),yymmdd10.) || "'");
end;
run;

%put &bom &end &current;

262    %put &bom &end &current;
'2015-12-01'  '2015-12-30'  '2016-01-07'

It would seem simple to apply this logic to a "quarterly type" condition. So - If the (sequential) day of the quarter is less than 8 days after the last day of the quarter, your &boq (beginning of quarter) value would be the first day of the LAST quarter'2015-10-01', but the qtr function creates values based on the quarter 1-4, not the "number" representing the sequential day of the quarter.

Is there a function that can operate on the number of days on a quarterly level, much like the day function operating on a monthly level?

My initial attempt was wrapping functions...no success...

qtr_day = day(qtr(today()));

Upvotes: 2

Views: 368

Answers (1)

Reeza
Reeza

Reputation: 21274

The trick is to use SAS date, since SAS dates are numbers, so you can find the date boundary and add/subtract 8 as desired to increment. Or you can nest intnx functions on a date. To have the date display as a quarter use a quarter format to display the date.

date_qtr_boundary = intnx('quarter', today(), 0, 'e') + 8;

Then you can compare your dates to the boundary value rather than the number 8. I'm having a hard time following exactly what you want to determine, but if you post some sample data and expected output, I (or someone else) can provide more details.

Upvotes: 4

Related Questions