Fancy charts with simple code in SAS 9.2 using SGPLOT

7

At SAS Global Forum last week, a customer approached me with a very specific request. The conversation went something like this:

Customer: My client demands a bar chart that uses a bar for one response, and a symbol for other responses, all on the same chart. We know it's possible to do with the GCHART procedure, but we don't want to have to use the fancy annotation code.

Me: Great! SAS has the GBARLINE procedure (Bar-Line chart in SAS Enterprise Guide) that supports something like that.

Customer: But in addition to the bar, we need multiple symbol measures.

Me: Ah, I see. In our current release GBARLINE supports just one symbol measure (line), but -- good news -- in SAS 9.2 it actually does support multiple lines.

Customer: Right, but the client wants the bars to be horizontal. GBARLINE supports just vertical bars.

The customer thought he had me stumped, but then I pulled the new SGPLOT procedure out of my bag of tricks.

In a few minutes I had adapted one of the examples from the SAS Online Documentation to show how this could be done. I used the HBAR statement to create the horizontal bar, and I used the HLINE statement (2 of them) to create "lines" with symbol markers. I hid the lines by assigning them a "thickness" of 0.

Here is the program I came up with:

proc sgplot data=sashelp.stocks (where=(date >= "01jan2000"d
  and date <= "01jan2001"d
  and stock = "IBM"));
hbar date /
  response=volume legendlabel="Volume";
hline date /
  response=close x2axis
  markers markerattrs=(size=12)
  lineattrs=(thickness=0) legendlabel="Close";
hline date /
  response=open x2axis
  markers markerattrs=(size=12)
  lineattrs=(thickness=0) legendlabel="Open";
x2axis label="Open/Close value";
xaxis label="Trade volume";
run;

And here is the result:
SGPLOT example

The graph team in R&D tells me that I could have achieved something similar with the DOT statement instead of my little trick to hide the line with a zero thickness.

One of the things that I love about the SGPLOT procedure is that all of the appearance attributes are either in the ODS style or in the procedure block itself -- no need to fuss with the global GOPTIONS like AXIS or LEGEND to get the graph looking like you want it. For more information about this and other statistical graphs, check out this SAS Global Forum paper.

Share

About Author

Chris Hemedinger

Senior Manager, SAS Online Communities

+Chris Hemedinger is the manager of SAS Online Communities. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies.  He also hosts the SAS Tech Talk webcasts each year from SAS Global Forum, connecting viewers with smart people from SAS R&D and the impressive work that they do.

7 Comments

  1. Another Chris on

    Nice chart!
    Last time someone asked me to do something like that I spent about 2 hours trying to set up graphs then using proc greplay to combine them together. Then I gave up and used Excel.
    THIS looks like a nice procedure.
    Is it 9.2 only? If so, I think I need to have a word to our SAS rep about when I can get my grubby little hands on it.

  2. Hi Chris,

    Nice work, I'm looking for a example where I can get how to plot
    MACD, Relative strength Index and DMI+ DMI- for stock market analysis using proc SGPLOT and to output that graph in Excel Sheet , Is there any book which could help me only for stock market analysis using Graph
    Can you post some example for above mention Stock Method using proc sgplot graph.

    Sunny

  3. You can create many types of graphs using SG procedures or GTL. They use a "layering" method to combine different plot types to create a graph. Graphs can be Single-Cell, Multi-Cell or Classification Panels.

    For examples see links below. For Doc, see V9.2 SAS/GRAPH SG Procedures and Graph Template language.

    Proc SGPLOT: http://support.sas.com/sassamples/graphgallery/PROC_SGPLOT.html

    Proc SGPANEL: http://support.sas.com/sassamples/graphgallery/PROC_SGPANEL.html

    Proc SGSCATTER: http://support.sas.com/sassamples/graphgallery/PROC_SGSCATTER.html

    GTL: http://support.sas.com/sassamples/graphgallery/PROC_SGRENDER_Graph_Template_Language__GTL_.html

  4. Pingback: This list goes to 11 - The SAS Dummy

Leave A Reply

Back to Top