BookmarkSubscribeRSS Feed
MelissaN
Obsidian | Level 7

I would like to format a datetime value as QX-YY, for example, Q1-19, Q2-19,...

Any help is much appreciated.

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

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.

Reeza
Super User
How are you planning to use this data? You could take two approaches, create a custom picture format or create a custom format. The custom picture format will not be honoured on graphs, but a custom format will be honoured on graphs.
MelissaN
Obsidian | Level 7

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.

Reeza
Super User
Creating charts in VA? or a different tool? Custom formats may not work in VA (unsure). If it's a different system entirely (Tableau, PowerBI) its different. The problem with creating a text variable is that it won't sort well. If you had 2018-Q1 that would sort fine, but since alphabetical sort is pretty much the default, you'd have all Q1s then Q2s then Q3s and Q4s order by year within each quarter.
MelissaN
Obsidian | Level 7
No, my team members use a different system to create chart. I may check with them to see if they could change the way they format the quarter to make it more efficient.
Kurt_Bremser
Super User

A format like this is sub-optimal in two ways:

  1. It uses a two-digit year. After the Y2K scare, it should be obvious that 2-digit years are a VERY BAD THING.
  2. It does not sort well on its own. Date/time values should always go from most significant to least significant. Using otherwise structured values will have hilarious results when used in eg graphs (I speak from experience).

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1363 views
  • 2 likes
  • 5 in conversation