BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
elolvido
Fluorite | Level 6

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!

 

1 ACCEPTED SOLUTION
4 REPLIES 4
elolvido
Fluorite | Level 6
Thanks very much for the prompt reply and an effective solution!
ballardw
Super User

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.

elolvido
Fluorite | Level 6
Ah yes, it is the end date I am rounding up to. Variable is a date not a datetime. Thanks very much for the solution and breakdown!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1819 views
  • 3 likes
  • 3 in conversation