Published on :
Macro CREATION_INTERNE

Graphic Resizing Macro

This code is also available in: Deutsch Español Français
This SAS© macro, `%gscale`, is designed to dynamically adjust the graphic dimensions (`hsize`, `vsize`) and positions (`hpos`, `vpos`) of SAS© graphs. Its main objective is to preserve the aspect ratio of graphic cells, thus ensuring that text and visual elements are not distorted during resizing. It accepts new values for height (h) and width (v), as well as a unit. The macro calculates the new hpos and vpos positions based on changes to hsize and vsize, then applies these parameters via the `goptions` statement.
Data Analysis

Type : CREATION_INTERNE


The macro operates on internal macro variables (`&hsize`, `&vsize`, `&hpos`, `&vpos`), which are either initialized by the SAS environment or defined by a prerequisite macro (%gask). The data processed are numerical values calculated within a `DATA _NULL_` step, without reading external datasets or the SASHELP library. The results of the calculations are then stored in macro variables via `call symput`.

1 Code Block
Macro Call
Explanation :
This call to the `%gask` macro (whose definition is not provided in this script) is used to retrieve the current values of the `hsize`, `vsize`, `hpos`, and `vpos` macro variables in the SAS environment. These values are essential for subsequent calculations to ensure that resizing is based on the current state of the graphic options.
Copied!
1%gask(unit=&unit);
2 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` step is the core of the resizing logic. It calculates the horizontal (`hcell`), vertical (`vcell`) cell sizes and their aspect ratio (`acell`). Conditional logic determines how `hsize` and `vsize` should be adjusted based on the `h` and `v` inputs. The `hpos` and `vpos` values are then recalculated to maintain the aspect ratio after the size change. Finally, `call symput` is used to update the global macro variables `hsize`, `vsize`, `hpos`, and `vpos` with the new values, making them available for subsequent SAS statements. Debugging information is displayed if the `verbose` parameter is enabled.
Copied!
1DATA _null_;
2 h=&h;
3 v=&v;
4 hcell = &hsize / &hpos; %*-- size of horizontal cell;
5 vcell = &vsize / &vpos; %*-- size of vertical cell;
6 acell = hcell / vcell; %*-- cell aspect ratio;
7 verbose = &verbose;
8 IF verbose THEN put hcell= vcell= acell=;
9
10 IF h=. or h=&hsize THEN DO;
11 IF v=. or v=&vsize THEN DO;
12 *-- nothing has changed;
13 goto done;
14 END;
15 ELSE DO;
16 *-- only vsize has changed;
17 vsize = v;
18 hsize = &hsize;
19 END;
20 END;
21 ELSE DO;
22 *-- hsize has changed;
23 IF v=. or v=&vsize THEN DO;
24 *-- only hsize has changed;
25 hsize = h;
26 vsize = &vsize;
27 END;
28 ELSE DO;
29 *-- both hsize, vsize have changed;
30 hsize = h;
31 vsize = v;
32 END;
33 END;
34* hpos = round(&hpos * (&vsize / vsize));
35* vpos = round(&vpos * (&hsize / hsize));
36 hpos = round(&hpos * (&hsize / hsize));
37 vpos = round(&vpos * (&vsize / vsize));
38 
39 call symput('hsize', compress(put(hsize,best6.2)));
40 call symput('vsize', compress(put(vsize,best6.2)));
41 call symput('hpos', compress(put(hpos,3.)));
42 call symput('vpos', compress(put(vpos,3.)));
43 
44 IF verbose THEN DO;
45 put hsize= vsize= hpos= vpos=;
46 hcell = hsize / hpos;
47 vcell = vsize / vpos;
48 acell = hcell / vcell;
49 put hcell= vcell= acell=;
50 END;
51 done:
52RUN;
3 Code Block
GOPTIONS
Explanation :
This `goptions` statement applies the `hsize`, `vsize`, `hpos`, and `vpos` values that were calculated and updated in the `DATA _NULL_` step. It thus defines the new dimensions and positioning of the graph using the specified unit, ensuring that the graph is displayed with the corrected aspect ratio.
Copied!
1goptions hsize=&hsize &unit vsize=&vsize &unit hpos=&hpos vpos=&vpos;
2 
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.