/
AreaBarDataMacros.sas
198 lines (186 loc) · 5.93 KB
/
AreaBarDataMacros.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/********************************************************************/
/* Macros for generating area bar chart data for PROC SGPLOT */
/********************************************************************/
/***************************************************/
/* Basic area bar chart */
/* notes: */
/* - The values for the response and width */
/* input variables are summed. */
/* - The response output column contains the */
/* response values for labeling. */
/* - The width output column contains the width */
/* values for labeling. */
/* */
/* args: */
/* input - input data set name */
/* output - output data set name */
/* category - category variable for each bar */
/* response - variable for the length of each bar */
/* width - variable for the width of each bar */
/***************************************************/
%macro genAreaBarDataBasic(input, output, category, response, width);
proc summary data=&input nway;
class &category;
var &response &width;
output out=_out_totals_ sum=;
run;
data &output;
retain x 0;
label x="&width" y="&response" ID="&category";
set _out_totals_;
ID=&category;
response=&response;
width=&width;
y=0;
x=x;
output;
y=&response;
output;
x = x + &width;
output;
y=0;
output;
run;
%mend;
/***************************************************************/
/* Area bar chart with color response */
/* notes: */
/* - The values for the response and width input */
/* variables are summed. */
/* - The color response is summed by default, but supports */
/* PROC SUMMARY statistics via the colorStat argument. */
/* - The response output column contains the */
/* response values for labeling. */
/* - The width output column contains the width */
/* values for labeling. */
/* */
/* args: */
/* input - input data set name */
/* output - output data set name */
/* category - category variable for each bar */
/* response - variable for the length of each bar */
/* width - variable for the width of each bar */
/* colorResp - continuous variable used to represent color */
/* colorStat (optional) - the SUMMARY statistic for colorResp */
/***************************************************************/
%macro genAreaBarDataColorResponse(input, output, category, response, width, colorResp, colorStat=sum);
proc summary data=&input nway;
class &category;
var &colorResp;
output out=_out_color_ &colorStat=_colorResponse_;
run;
proc summary data=&input nway;
class &category;
var &response &width;
output out=_out_totals_ sum=;
run;
data _merged_;
merge _out_totals_ _out_color_;
by &category;
run;
data &output;
retain x 0;
label x="&width" y="&response" ID="&category" colorResponse="&colorResp";
set _merged_;
colorResponse=_colorResponse_;
ID=&category;
response=&response;
width=&width;
y=0;
x=x;
output;
y=&response;
output;
x = x + &width;
output;
y=0;
output;
run;
%mend;
/***************************************************/
/* Area bar chart with subgroups */
/* notes: */
/* - The values for the response and width */
/* variables are summed. */
/* - Use TEXT plot statements to display the */
/* subgroup labels and bar labels. */
/* - The generated subLabel, subLabelX, and */
/* subLabelY variables can be used to display */
/* the subgroup values in each segment. */
/* - The generated label, labelX, and labelY */
/* variables can be used to display the */
/* category values above each bar. */
/* */
/* */
/* args: */
/* input - input data set name */
/* output - output data set name */
/* category - category variable for each bar */
/* response - variable for the length of each bar */
/* width - variable for the width of each bar */
/* subgroup - variable for the bar segments */
/***************************************************/
%macro genAreaBarDataSubgroup(input, output, category, response, width, subgroup);
proc summary data=&input nway;
class &category;
var &width;
output out=_out_width_ sum=;
run;
proc summary data=&input nway;
class &category &subgroup;
var &response;
output out=_out_totals_ sum=;
run;
data _merged_;
merge _out_totals_ _out_width_;
by &category;
run;
data &output;
retain curCategory curLabelX curLabelY;
retain x 0 y 0 prevY 0 nextY 0 prevX 0;
label x="&width" y="&response" ID="&subgroup";
set _merged_ end=_last_;
ID=&subgroup;
if (curCategory ne &category) then do;
if x > 0 then do;
label = curCategory;
labelX = curLabelX;
labelY = curLabelY;
end;
curCategory = &category;
y=0;
prevY=0;
end;
else do;
y=nextY;
prevY = y;
x=prevX;
end;
output;
labelX=.;
labelY=.;
y = y + &response;
nextY = y;
output;
prevX = x;
x = x + &width;
output;
subLabelX = prevX + ((x-prevX) / 2);
subLabelY = prevY + ((y-prevY) / 2);
subLabel = &response;
curLabelX = prevX + ((x-prevX) / 2);
curLabelY = prevY + (y-prevy);
y=prevY;
output;
subLabelX=.;
sunLabelY=.;
if (_last_) then do;
x=.;
y=.;
label = curCategory;
labelX = curLabelX;
labelY = curLabelY;
output;
end;
run;
%mend;