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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.