Technology Tales

Adventures in consumer and enterprise technology

Creating waterfall plots in SAS using PROC GCHART

Published on 17th March 2012 Estimated Reading Time: 2 minutes

Recently, I needed to create a waterfall plot couldn't use PROC SGPLOT since it was incompatible with publishing macros that use PROC GREPLAY on the platform that I was using; SGPLOT doesn't generate plots in SAS catalogues but directly creates graphics files instead. Therefore, I decided that PROC GCHART needed to be given a go, and it delivered what was needed.

The first step is to get the data into the required sort order:

proc sort data=temp;
    by descending result;
run;

Then, it is time to add an ID variable for use in the plot's X-axis (or midpoint axis in PROC GCHART) using an implied value retention to ensure that every record in the dataset had a unique identifier:

data temp;
    set temp;
    id+1;
run;

After that, axes have to be set up as needed. For instance, the X-axis (the axis2 statement below) needs to be just a line with no labels or tick marks on there and the Y-axis was fully set up with these, turning the label from vertical to horizontal as needed with the ANGLE option controlling the overall angle of the word(s) and the ROTATE option dealing with the letters, and a range declaration using the ORDER option.

axis1 label=none major=none minor=none value=none;
axis2 label=(rotate=0 angle=90 "Result") order=(-50 to 80 by 10);

With the axis statements declared, the GCHART procedure can be defined. Of this, the VBAR statement is the engine of the plot creation, with the ID variable used for the midpoint axis and the result variable used as the summary variable for the Y-axis. The DISCRETE keyword is needed to produce a bar for every value of the ID variable, or GCHART will bundle them by default. Next, references for the above axis statements (MAXIS option for midpoint axis and AXIS option for Y-axis) are added, and the plot definition is complete. One thing that has to be remembered is that GCHART uses run group processing, so a QUIT statement is needed at the end to close it at execution time. This feature has its uses and appears in other procedures too, though SAS procedures generally are concluded by a RUN statement.

proc gchart data=temp;
    vbar id / sumvar=result discrete axis=axis2 maxis=axis1;
run;
quit;
  • The content, images, and materials on this website are protected by copyright law and may not be reproduced, distributed, transmitted, displayed, or published in any form without the prior written permission of the copyright holder. All trademarks, logos, and brand names mentioned on this website are the property of their respective owners. Unauthorised use or duplication of these materials may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties.

  • All comments on this website are moderated and should contribute meaningfully to the discussion. We welcome diverse viewpoints expressed respectfully, but reserve the right to remove any comments containing hate speech, profanity, personal attacks, spam, promotional content or other inappropriate material without notice. Please note that comment moderation may take up to 24 hours, and that repeatedly violating these guidelines may result in being banned from future participation.

  • By submitting a comment, you grant us the right to publish and edit it as needed, whilst retaining your ownership of the content. Your email address will never be published or shared, though it is required for moderation purposes.