Advanced ODS Graphics: Title change macro

3

Have you ever wanted to modify a graph title that is produced by an analytical procedure? You can make a wide variety of changes by modifying the graph template. Modifying the graph template is straight forward. You specify ODS TRACE ON, run the procedure, find the template name, display the template, modify the template, submit it, and rerun the procedure. I will show you a different way. This blog, the macros, and the examples were revised on May 31, 2017.

First, consider running PROC LOGISTIC as follows:

data Neuralgia;
   input Treatment $ Sex $ Age Duration Pain $ @@;
   datalines;
P F 68 1 No B M 74 16 No P F 67 30 No P M 66 26 Yes B F 67 28 No B F 77 16 No A
F 71 12 No B F 72 50 No B F 76 9 Yes A M 71 17 Yes A F 63 27 No A F 69 18 Yes B
F 66 12 No A M 62 42 No P F 64 1 Yes A F 64 17 No P M 74 4 No A F 72 25 No P M
70 1 Yes B M 66 19 No B M 59 29 No A F 64 30 No A M 70 28 No A M 69 1 No B F 78
1 No P M 83 1 Yes B F 69 42 No B M 75 30 Yes P M 77 29 Yes P F 79 20 Yes A M 70
12 No A F 69 12 No B F 65 14 No B M 70 1 No B M 67 23 No A M 76 25 Yes P M 78 12
Yes B M 77 1 Yes B F 69 24 No P M 66 4 Yes P F 65 29 No P M 60 26 Yes A M 78 15
Yes B M 75 21 Yes A F 67 11 No P F 72 27 No P F 70 13 Yes A M 75 6 Yes B F 65 7
No P F 68 27 Yes P M 68 11 Yes P M 67 17 Yes B M 70 22 No A M 65 15 No P F 67 1
Yes A M 67 10 No P F 72 11 Yes A F 74 1 No B M 80 21 Yes A F 69 3 No
;
 
ods trace on;
ods graphics on;
proc logistic data=Neuralgia;
   class Treatment Sex / param=glm;
   model Pain= Treatment|Sex Age;
   lsmeans Treatment / plots=anom;
run;

Click on a graph to enlarge.

anomplot

Here is the trace output for the analysis of means plot:

Name:       AnomPlot
Label:      Treatment ANOM Plot
Template:   Stat.Logistic.Graphics.AnomPlot
Path:       Logistic.LSMeans.AnomPlot

You can use a macro to modify the way that the template creates titles as follows:

%grtitle(path=Stat.Logistic.Graphics.AnomPlot)

The macro displays the following:

Stat.Logistic.Graphics.AnomPlot -> Stat.Graphics.AnomPlot
   Graphics_AnomPlot
   Graphics_AnomPlot2

The macro begins by displaying the name of the template that you specified in the PATH= argument. In this case, two names are displayed, because the specified template is a link. The macro follows links and modifies the actual graph template that is at the end of the link chain. The macro creates and displays the names of two macro variables (since there are two titles) after the template names. The modified template creates titles from the values of the macro variables when those variables exist; it uses the original titles when the macro variables do not exist. The names of the macro variables are constructed from the second and last levels of the template name. In many cases, these two levels are the procedure name and the graph name (but not in this case because of the link).

The following step creates the graph using the modified template and the two macro variables.

%let Graphics_AnomPlot = Neuralgia Study;
%let Graphics_AnomPlot2 = Analysis of Means with 95% Decision Limits;
proc logistic data=Neuralgia;
   ods select anomplot;
   class Treatment Sex / param=glm;
   model Pain= Treatment|Sex Age;
   lsmeans Treatment / plots=anom;
run;

anomplot1

Here is how it works. The original template has two ENTRYTITLE statments:

entrytitle _TITLE;
entrytitle textattrs=GRAPHVALUETEXT _CLSTR;

The macro modifies the template as follows:

mvar Graphics_AnomPlot Graphics_AnomPlot2;
if (EXISTS(GRAPHICS_ANOMPLOT))
   entrytitle GRAPHICS_ANOMPLOT;
else
   entrytitle _TITLE;
endif;
if (EXISTS(GRAPHICS_ANOMPLOT2))
   entrytitle GRAPHICS_ANOMPLOT2;
else
   entrytitle textattrs=GRAPHVALUETEXT _CLSTR;
endif;

If the macro variable exists, your title is used, otherwise the original title is used. If the macro variable exists but has a null value (%LET Graphics_AnomPlot = ;), that title is suppressed. Notice that there are no ampersands. Macro variable values are substituted at the time that PROC LOGISTIC is run not at the time that the macro runs PROC TEMPLATE.

The modified template is in an item store in the WORK library. You can delete that item store as follows:

%grtitle(options=delete)

You can modify multiple templates at once. The three steps below modify all templates, all STAT templates, and all PROC LOGISTIC templates.

%grtitle;
%grtitle(path=stat)
%grtitle(path=stat.logistic)

The following step lists all of the modified templates that are in the WORK library:

proc template; list / store=work.templat; quit;

The macro header provides additional documentation. The macro does the following:

  • All modified templates in the WORK library are deleted.
  • PROC TEMPLATE writes the graph templates to a file.
  • DATA steps follow the links.
  • A DATA step modifies the template code and submits it to SAS using CALL EXECUTE.

Macro Code: Graph Title Macro

You can use this code as a prototype for other forms of customization. You could just as easily add DRAW statements that add watermarks or logos to every graph. SAS provides all of the tools that you need to easily access all of the templates and systematically modify them.

Share

About Author

Warren F. Kuhfeld

Distinguished Research Statistician

Warren F. Kuhfeld is a distinguished research statistician developer in SAS/STAT R&D. He received his PhD in psychometrics from UNC Chapel Hill in 1985 and joined SAS in 1987. He has used SAS since 1979 and has developed SAS procedures since 1984. Warren wrote the SAS/STAT documentation chapters "Using the Output Delivery System," "Statistical Graphics Using ODS," "ODS Graphics Template Modification," and "Customizing the Kaplan-Meier Survival Plot." He also wrote the free web books Basic ODS Graphics Examples and Advanced ODS Graphics Examples.

Related Posts

3 Comments

  1. Pingback: Advanced ODS Graphics: Remove ODS Subtitles - Graphically Speaking

  2. What version of SAS are you using? My SAS version (9.3) does not seem to recognize the %grtitle macro.

    • Warren F. Kuhfeld
      Warren F. Kuhfeld on

      You will need to use the macro that I provided at the end of the blog. I wrote the macro for the blog. It is not available in current SAS releases.

Back to Top