Long category values


The South East SAS Users Group meeting wound up yesterday.  The 25th anniversary conference was held on SAS Campus and it provided a great opportunity to meet with many enthusiastic SAS users and attend many informative presentations.  More on this in a follow-up article.

During one of these presentations, Mary Beth from Rho shared with me a graph that had rather long category values on the y-axis, and wanted to know how we would go about doing that using SGPlot procedure.

This also fitted in nicely with another conversation on the SAS communities page on how to deal with long category values.  This user wanted to display the long values on the x-axis, using diagonal arrangement, but wanted an angle other than 45 degrees.

The SGPLOT procedure automatically uses a "splitting" technique to display long category values on the x-axis.  If the number of categories is moderate (5-8), this works well as the category value can be split on "white" space.  This usually works for most cases.  I discussed this in a recent article on Graphs with Long Category Values. However, it may be useful to review this specific case here.

Sometimes, however, you may have really long category values, such as for a survey questionnaire.  Or, you may have category values that do not have white spaces, such as URLs.  In such cases, often it may be better to place the category variable on the y-axis.

By default, the graph will try to assign as much of the horizontal space available to the y-axis tick values.  In this case, the will leave no room for the bar chart itself.  You can use the FITPOLICY=Split option, which will assign about 25% of the width to the axis, and split each value at the appropriate white space to display the graph.  This too may not work in this case with long survey values would spread across the space for each bar and overlap.

In this case, the best process would be to insert a special "split character" in the text string where the string can be split.  If you view the data set in the link below, you will see I inserted the "^" character in each survey answer to split the string where I want.  I also provide this split character to the axis using the option SPLITCHAT='^'.  While I inserted only one split character, you can insert more.  The string will be split at each occurrence of the split character(s).  More number of splits will spread the value across more space vertically.  By default, the split character will be dropped.  But if needed, it can be retained by setting the SPLITCHARNODROP option.

proc sgplot data=survey noborder;
    hbar question / response=response stat=mean nostatlabel dataskin=pressed
            displaybaseline=auto filltype=gradient fillattrs=graphdata1
            datalabel datalabelattrs=(size=8) barwidth=0.7;
    yaxis fitpolicy=split display=(nolabel noticks noline) splitchar='^'
           valueattrs=(size=8) discreteorder=data;
    xaxis display=(noline noticks nolabel) grid valueattrs=(size=8);

I also used the FILLTYPE=Gradient option to get a gradient fill effect for the bars.

SAS SGPLOT code:  longCategoryValues


About Author

Sanjay Matange

Director, R&D

Sanjay Matange is R&D Director in the Data Visualization Division responsible for the development and support of the ODS Graphics system, including the Graph Template Language (GTL), Statistical Graphics (SG) procedures, ODS Graphics Designer and related software. Sanjay has co-authored a book on SG Procedures with SAS/PRESS.

Back to Top