Published on :
Macro MIXTE

Post-generation analytical graph annotation

This code is also available in: Deutsch Français
Awaiting validation
This script uses a %procanno macro to modify an existing graph stored in an ODS document. It first extracts the data, graph template, and dynamic variables from the original graph. Then, it dynamically modifies the template to include an annotation instruction. Finally, it uses PROC SGRENDER to redraw the graph with the original data, the modified template, and an overlaid annotation dataset, allowing a watermark and a date to be added.
Data Analysis

Type : MIXTE


The script uses the SASHELP.CLASS table for the initial regression. It then internally creates an annotation dataset (anno), and a dataset (dp) by capturing the graphical output of PROC REG.

1 Code Block
PROC REG Data
Explanation :
Performs a simple linear regression on sashelp.class. The ODS output is captured: the diagnostics panel is saved to a SAS dataset named 'dp', and the complete graphical object is stored in an ODS document named 'MyDoc' for later processing.
Copied!
1ods graphics on;
2ods document name=MyDoc (write);
3PROC REG DATA=sashelp.class;
4 ods select diagnosticspanel;
5 ods OUTPUT diagnosticspanel=dp;
6 model weight = height;
7QUIT;
8ods document close;
2 Code Block
DATA STEP Data
Explanation :
Creates an annotation dataset ('anno') that defines two textual elements to overlay on a graph: a red date in the top right corner and a diagonal 'Confidential' watermark with high transparency.
Copied!
1DATA anno;
2 LENGTH Label $ 40;
3 Function = 'Text'; Label = 'Saturday, July 25, 2015';
4 Width = 100; x1 = 99; y1 = .1;
5 Anchor = 'Right'; TextColor = 'Red';
6 OUTPUT;
7
8 Label = 'Confidential - Do Not Distribute';
9 Width = 150; x1 = 50; y1 = 50; Anchor = 'Center';
10 Transparency = 0.8; TextSize = 40; Rotate = -45;
11 OUTPUT;
12RUN;
3 Code Block
MACRO
Explanation :
Defines a '%procanno' macro. It takes a dataset, a template name, an annotation dataset, and an ODS document as parameters. Its role is to extract the components of an existing graph (data, template, dynamic variables), modify its template to insert an annotation layer, then regenerate it with PROC SGRENDER by applying the annotations.
Copied!
1%macro procanno(DATA=, template=, anno=anno, document=mydoc);
2 PROC DOCUMENT name=&document;
3 ods exclude properties;
4 ods OUTPUT properties=__p(where=(type='Graph'));
5 list / levels=all;
6 QUIT;
7 
8 DATA _null_;
9 SET __p;
10 call execute("proc document name=&document;");
11 call execute("ods exclude dynamics;");
12 call execute("ods output dynamics=__outdynam;");
13 call execute(catx(' ', "obdynam", path, ';'));
14 RUN;
15 
16 PROC TEMPLATE;
17 SOURCE &template/ file='temp.tmp';
18 QUIT;
19 
20 DATA _null_;
21 INFILE 'temp.tmp';
22 INPUT;
23 IF _n_ = 1 THEN call execute('proc template;');
24 call execute(_infile_);
25 IF _infile_ =: ' BeginGraph' THEN bg + 1;
26 IF bg and index(_infile_, ';') THEN DO;
27 bg = 0;
28 call execute('annotate;');
29 END;
30 RUN;
31 
32 DATA _null_;
33 SET __outdynam(where=(label1 ne '___NOBS___')) END=eof;
34 IF nmiss(nvalue1) and cvalue1 = '.' THEN cvalue1 = ' ';
35 IF _n_ = 1 THEN DO;
36 call execute("proc sgrender data=&data sganno=&anno");
37 call execute("template=&template;");
38 call execute('dynamic');
39 END;
40 IF cvalue1 ne ' ' THEN
41 call execute(catx(' ', label1, '=',
42 ifc(n(nvalue1), cvalue1, quote(trim(cvalue1)))));
43 IF eof THEN call execute('; run;');
44 RUN;
45 
46 PROC TEMPLATE;
47 delete &template;
48 QUIT;
49%mend;
4 Code Block
MACRO
Explanation :
Calls the '%procanno' macro to apply the annotation process. It uses the 'dp' dataset (containing the graph data) and specifies the 'Stat.REG.Graphics.DiagnosticsPanel' template as the basis for reconstructing the annotated graph.
Copied!
1%procanno(
2DATA=dp, template=Stat.REG.Graphics.DiagnosticsPanel)
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Banner
Expert Advice
Expert
Stéphanie
Spécialiste Machine Learning et IA.
« his technique is the "nuclear option" for graph customization. It allows you to bypass the limitations of built-in procedure options, effectively turning SAS into a fully automated desktop publishing engine for statistical results. »