Consider this picture format:
proc format;
picture myqtr (default=6)
low-high='%q%Y' (datatype=date)
;
run;
applied to this dataset:
data have;
infile datalines dlm="09"x;
input phase $ patients qtr :yymmdd10.;
format qtr myqtr.;
datalines;
I 5 2021-01-01
I 10 2021-04-01
I 14 2021-07-01
I 8 2022-01-01
II 20 2021-01-01
II 30 2021-04-01
II 66 2021-07-01
II 55 2022-01-01
III 60 2021-01-01
III 70 2021-04-01
III 78 2021-07-01
III 67 2022-01-01
;
In the dataset, the dates show nicely as (e.g) Q12021
Now I run PROC SGPLOT:
proc sgplot data=have;
label
patients = "Patients"
qtr = "Quarter"
;
vline qtr / response=patients group=phase;
yaxis grid;
run;
and I get this:
Just the format string is used as value.
But when I change the format to a standard SAS format:
proc sgplot data=have;
format qtr yyq7.;
label
patients = "Patients"
qtr = "Quarter"
;
vline qtr / response=patients group=phase;
yaxis grid;
run;
I get this:
Does SGPLOT have an issue with custom formats?
Found it. The Graph Template Language cannot deal with the directives of a date, time or datetime picture format:
Found it. The Graph Template Language cannot deal with the directives of a date, time or datetime picture format:
From Googling this problem, it looks like others have reported similar behavior. See
First, I think you will have more success by keeping a native SAS format. Otherwise, the X axis doesn't know how to handle the spacing for the dates. Remove the FORMAT statement and use the
XAXIS valuesformat=
option to supply a custom format.
Second, it looks like the VALUESFORMAT= option does not work with a PICTURE format, so unfortunately you cannot use
xaxis valuesformat=myqtr.; /* does not work with PICTURE format */
I believe it works for other custom formats, but I have not tested it. Try creating your desired format by using a conventional VALUE statement in PROC FORMAT or by defining an FCMP function.
Custom formats work, but not picture formats that use directives. I have not looked into this, but I suspect that the directive notation would interfere with the GTL syntax (the % character is the usual suspect here).
Native formats are no problem at all, but a user wanted to have a different notation of quarters that is not covered by a standard SAS format, see in this thread: https://communities.sas.com/t5/SAS-Programming/How-to-depict-quarters-of-the-year-on-the-x-axis/td-p...
It would still be nice, though, to have all elements in SAS treat all formats the same, consistently. After all, it took me (and I'm no SAS newbie) considerable effort to find the reason for the non-function. And, IMHO, as a customer of SAS one would expect the software to be consistent throughout with the use of formats. I consider this expectance part of the big bucks people pay for SAS.
PS Da haben sich die Designer nicht mit Ruhm bekleckert.
("The designers did not exactly cover themselves with glory")
I might suggest using a function created in FCMP to produce a string value for the target of a format.
Of course that adds another level of headache to code maintenance but the things we do for customers...
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.