I would like to format a datetime value as QX-YY, for example, Q1-19, Q2-19,...
Any help is much appreciated.
As far as I know, there is no format QX-YY.
There are formats YY/X or YY-X or YYQX. So unless you have some very picky and precise requirements, I wouldn't bother trying to get QX-YY. If you absolutely have to have QX-YY, then you probably need to create a new characater variable to match this exact appearance.
If year followed by quarter is acceptable, you might consider using the YYQ format.
A custom format can create that though.
Proc format library=work; picture qyy low-high ='Q%q-%0y' (datatype=date) ; run; data example; input date date9.; datalines; 01Jan1960 23Feb2001 18Dec1901 ; proc print data=example; format date qyy.; run;
Notice that I intentionally picked example dates that might bring the year value into question as I hate two-digit years as almost anyone who worked on Y2K data issues likely does.
You will need to make sure the format is available whenever you need it.
I am using this data to upload to one of our data source, and then we will use it for creating charts, so I think a custom format is what I am looking for.
Thanks.
A format like this is sub-optimal in two ways:
So you should go for YYYY-Qx, which is best solved by creating this custom format:
proc format;
picture myquarter
low-high ='%Y-Q%q' (datatype=date)
;
run;
data test;
x1 = put(today(),myquarter7.);
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.