- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I'm fairly new to SAS coming from R and am adapting someone else's previous code for my project. In a large dataset with dates, we must round the date to the nearest quarter, which was previously done as
data xyz;
set xyz;
date_rounded=round(date,90);
format date_rounded DATE11.;
run;
This works great, and I want to do the exact same thing, but only allowing upwards rounding (so if they are even a couple days into quarter 2, it will be rounded to quarter 3's date). I don't see an obvious way to do this since the CEIL function doesn't allow for a second argument specifying the rounding unit. However, this dataset is massive and covers over 15 years, so rather than macgyvering a clumsy solution using my limited SAS, I am hoping there is a simple/efficient way to do this.
Please let me know if you have any suggestions!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is done with the INTNX() function:
date_rounded = intnx('quarter',date,0,'e');
which gives you the correct calendar end date for the quarter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is done with the INTNX() function:
date_rounded = intnx('quarter',date,0,'e');
which gives you the correct calendar end date for the quarter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The question is what result you actually want as you don't say.
The proper function would likely be the INTNX function.
Result= intnx('quarter', datevariable,0,'B') would provide the first day (begining) of the calendar quarter the date occurs in.
Result= intnx('quarter',datevaraible,0,'E') would provide the last day (end) of the calendar quarter the date occurs in.
Result = intnx('quarter',datevaraible, 1,'B') would provide the first day of the next calendar quarter. I think this might be the one you want but provided the others just in case.
If your variable is actually a datetime you would use DTQUARTER instead. It is amazing how many people do not differentiate between "date" and "datetime" values.
Reason to use INTNX is that it will account for possibly issues with leap days.
And the companion function INTCK returns number of intervals between to dates, time or datetime values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content