Published on :
Reporting CREATION_INTERNE

Electricity Consumption History with GCHART

This code is also available in: Deutsch Español Français
Awaiting validation
This script creates a consumption (kWh) dataset via DATALINES. It then prepares custom axis labels by concatenating text in a DATA step and storing them in a macro variable via PROC SQL. Finally, it generates an HTML file containing a PROC GCHART with a logo added via an annotation dataset (ANNO).
Data Analysis

Type : CREATION_INTERNE


The main data is created manually in the DATA step using the DATALINES instruction.

1 Code Block
INITIALISATION
Explanation :
Definition of macro variables for the file name and target date, and definition of the output fileref.
Copied!
1%let name=example11;
2filename odsout '.';
3 
4%let targetdate=15sep2003;
2 Code Block
DATA STEP Data
Explanation :
Creation of the 'my_data' dataset containing dates and kwh consumption. Creation of a 'billmonth' indicator for the target date.
Copied!
1DATA my_data;
2FORMAT kwh comma5.0;
3FORMAT date mmyy5.;
4INPUT date date9. kwh;
5IF date eq "&targetdate"d THEN billmonth=1;
6ELSE billmonth=0;
7DATALINES;
815sep2002 800
915oct2002 550
1015nov2002 200
1115dec2002 190
1215jan2003 250
1315feb2003 200
1415mar2003 225
1515apr2003 190
1615may2003 325
1715jun2003 350
1815jul2003 675
1915aug2003 775
2015sep2003 875
21;
22RUN;
3 Code Block
PROC SORT
Explanation :
Sorting of data by chronological date.
Copied!
1PROC SORT DATA=my_data out=my_data;
2BY date;
3RUN;
4 Code Block
DATA STEP Data
Explanation :
Iterative construction of a complex character string ('axis_text') to define custom axis labels (every other month displayed).
Copied!
1DATA my_data; SET my_data;
2LENGTH axis_text $200;
3retain axis_text;
4IF mod(_n_,2)=1 THEN
5 axis_text=trim(left(axis_text))||' t='||trim(left(_n_))||' '||quote(put(date,monname3.));
6ELSE
7 axis_text=trim(left(axis_text))||' t='||trim(left(_n_))||' '||quote(' ');
8RUN;
5 Code Block
PROC SQL
Explanation :
Extraction of the complete label string constructed previously into a macro variable: :axis_text.
Copied!
1 
2PROC SQL;
3select axis_text into :axis_text from my_data having date=max(date);
4QUIT;
5 
6RUN;
7 
6 Code Block
DATA STEP Data
Explanation :
Creation of a SAS/GRAPH annotation dataset to insert a logo image ('power_logo.png') on the graph.
Copied!
1DATA logo_anno;
2 LENGTH function $8;
3 xsys='3'; ysys='3'; when='a';
4 function='move'; x=0; y=92; OUTPUT;
5 function='image'; x=x+36.4; y=y+7; imgpath='power_logo.png'; style='fit'; OUTPUT;
6RUN;
7 Code Block
PROC GCHART
Explanation :
Configuration of graphic options (GOPTIONS), opening of the ODS HTML destination, and generation of a vertical bar chart (VBAR) with data, logo annotations, and custom axes.
Copied!
1GOPTIONS DEVICE=png;
2goptions xpixels=480 ypixels=384;
3goptions border;
4
5ODS LISTING CLOSE;
6ODS HTML path=odsout body="&name..htm" (title="Power Bill") style=minimal;
7
8goptions gunit=pct htitle=6 ftitle="albany amt/bold" htext=4.5 ftext="albany amt/bold";
9 
10title1 h=8 " ";
11/* ... titres ... */
12title6 j=c h=6 "kWh Usage History";
13 
14axis1 label=none offset=(5,5) value=( &axis_text );
15axis2 label=none minor=none major=(number=5);
16 
17pattern1 v=x1 c=graybb;
18pattern2 v=x2 c=black;
19 
20PROC GCHART DATA=my_data anno=logo_anno;
21vbar date / discrete type=sum sumvar=kwh
22 subgroup=billmonth nolegend
23 maxis=axis1 raxis=axis2 noframe
24 width=5 space=2.25 coutline=black
25 autoref cref=graydd clipref
26 des="" name="&name";
27RUN;
28 
29QUIT;
30ODS HTML CLOSE;
31ODS LISTING;
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.