Data visualization with SAS programming


Posts: 42


Hi All,

I am plotting some descriptive statistics of a variable over time to visualize the variable coverage.

X axis is the month.It spans from 2004-01-01 to 2014-05-01 on a monthly basis.

I am using SGPLOT on sas 9.3 and here is the code I am using. (The data is also attached below).

proc sgplot data=percentiles;

where portfolio='AAA';

series x=date y=score_p5       / lineattrs=(color=CXAAA03E    thickness=2 pattern=solid) curvelabel='P05'  curvelabelloc=inside curvelabelpos=max;

series x=date y=score_p25      / lineattrs=(color=CXff750         thickness=2 pattern=solid) curvelabel='P25'  curvelabelloc=inside curvelabelpos=max;

series x=date y=score_mean     / lineattrs=(color=CX84351D   thickness=2 pattern=solid) curvelabel='MEAN' curvelabelloc=inside curvelabelpos=max;

series x=date y=score_p50      / lineattrs=(color=CXD1836D    thickness=2 pattern=solid) curvelabel='P50'  curvelabelloc=inside curvelabelpos=max;

series x=date y=score_p75      / lineattrs=(color=CX5A1804     thickness=2 pattern=solid) curvelabel='P75'  curvelabelloc=inside curvelabelpos=max;

series x=date y=score_p95      / lineattrs=(color=black             thickness=2 pattern=solid) curvelabel='P95'  curvelabelloc=inside curvelabelpos=max;

needle x=date y=score_miss_pct / y2axis lineattrs=(thickness=3 color=CX4D946E ) transparency=.70;

xaxis label="Month"  grid values=("01Jan2004"d to "01Dec2015"d by 1) interval=month type=discrete fitpolicy=rotatethin;

yaxis  label="SCORE";

y2axis grid label="Percent Missing";

title "VAR = Score; PORTFOLIO=AAA";


I need help with the following:

1. The date on the x axis is at an angle. I want to rotate that vertically. In SAS GRAPH there was a rotate option. I need to find a way to do that in SGPLOT.

2. I want the title inside of the box.

3. In the data (attached below), there are two portfolio - AAA and BBB. This plot is only for AAA. I want to show the same plot but in a panel for both the portfolios. This will help me see it across different portfolio. I tried

   SGPANEL but with not much success. I am hoping someone could help. I have not used GTL but would be open to that if there is a way to display this in a panel.

4.Eventually i want this to output on a PDF. In the PDF however, the image seems a little distorted. I have tried DPI but still it is not as good. Please share tips that has worked for you.

I would appreciate any help/suggestions, codes would be even better Smiley Happy

Thank you,

Super User
Posts: 11,134


Which version of SAS are you running? The SG graphics options have changed significantly with each release since 9.1.

1) Axis suggestions

Since the dates you are showing all end in -01 I would change the format used to lose that part of the display as it isn't adding anything, possibly yymmd. (the single d means dash between elements)

format date yymmd. ; anywhere in the proc statement or make it the default format when reading the data.

Change the values used for the tick marks, maybe to a by year interval such as values=("01Jan2004"d to "01Jan2014"d by year). I'm not sure that your actually adding much by having the additional tick marks.

2) Not finding an easy solution but if you are using 9.3 or later you could an annotation dataset and add the SGANNO= option on the proc statement to use that dataset to write text where you want it instead of a title statement.

3) With the number of items you are displaying I suspect anything that crams two similar plots into a single one might get cramped. I would use a BY statement to create two graphs and see if that works well enough. If the graph size is fixed you might get both onto a single PDF page close enough to almost resemble a panel.

4) sometimes turning off HTML output, ODS HTML Close, before generating PDF may make the output more PDF friendly.

Also using ODS GRAPHICS height and width options to control the graph size explicitly may help.

Posts: 925


Some thoughts:

1. Axis -- Do not set the axis type to DISCRETE and remove the VALUES option. The normal TIME axis will give a better representation given the density of your data. Also, by using the TIME axis, the INTERVAL option will work for you.

2 TItle -- The title is outside of the box because you are using the NOGTITLE option on the ODS HTML statement

3. SGPANEL -- Because you are using a secondary axis, you will not be able to use SGPANEL. However, you have two options:

     a. Use SGPLOT with a BY statement, and use ODS LAYOUT to position the graphs side-by-side

     b. Use a DATAPANEL or DATALATTICE layout in GTL

4. Are you using ODS PDF to generate the output, or are you putting the output into a PDF document yourself?

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation