Data visualization with SAS programming

proc sgplot bar-line graph and x-axis

Reply
Frequent Contributor
Posts: 104

proc sgplot bar-line graph and x-axis

Using proc sgplot I created a bar-line graph with a primary y-axis and a secondary y-axis.  The problem is my x-axis which is a date variable.  I would like to format the date using format mmyyd. However I can't seem to figure out how when I have multiple y-axes.  I have tried a couple of combinations but none seem to work (see two examples below).  I have also included my base code for the entire proc sgplot code.  (On an unrelated note, any idea why keylegend comes up in red on my code?  This line works but the words are in red as opposed to the expected blue.)

1. format date mmyyd. (I prefer this approach as my date ranges vary for each output.)

2. xaxis label="Date" fitpolicy=rotate  type=time values=('01Oct2010'd to '01Jan2013'd by month)

proc sgplot data=graphs;

vbar date / response=frequency_1;

vbar date / response=frequency_2 barwidth=0.5 transparency=0.2;

vline date/ response=percent_1 y2axis;

vline date/ response=percent_2 y2axis;

keylegend / across=2;

run;

Thanks!

Super Contributor
Posts: 543

Re: proc sgplot bar-line graph and x-axis

Hi.

What happens when you try thisSmiley Sadplease note the addition of the line in bold)

proc sgplot data=graphs;

vbar date / response=frequency_1;

vbar date / response=frequency_2 barwidth=0.5 transparency=0.2;

vline date/ response=percent_1 y2axis;

vline date/ response=percent_2 y2axis;

keylegend / across=2;

format date mmyyd.;

run;

Good luck!

Anca.

Frequent Contributor
Posts: 104

Re: proc sgplot bar-line graph and x-axis

When I use the format date mmyyd. statement, it does format my dates on the x-axis to the desired format. However, it changes the values percent (y2 axis) displayed on the graph.  I have found that it sums the percent values rather than just changing the look of the dates. 

For example, here are weekly percent values:

Dec 1 2011 = 8%

Dec 8 2011 = 12%

Dec 15 2011 = 4%

Dec 22 2011 = 0%

Dec 29 2011 = 5%

Using format date mmyd. - the % value on the y2 axis = 29% which is incorrect as the max % value for the month of December is 12%.  Does this make sense?

Super Contributor
Posts: 543

Re: proc sgplot bar-line graph and x-axis

Could you provide a small sample of your data, so I can test it?

Thanks.

Anca,

Frequent Contributor
Posts: 104

Re: proc sgplot bar-line graph and x-axis

Thank you for your help.  Please find attached a sample of the data.

Super Contributor
Posts: 543

Re: proc sgplot bar-line graph and x-axis

Hi,Sophia.

From what I can understand, when you format the x-axis to 'collapse' the weeks of a month into month...the default is to use the "sum" of the values associated with the weeks...

you could change that by using stat = mean in your vline statement.

proc sgplot data=temp;

vbar date / response=frequency_1 ;

vbar date / response=frequency_2 barwidth=0.5 transparency=0.2;

vline date/ response=percent_1 y2axis stat = mean;

vline date/ response=percent_2 y2axis stat = mean;

keylegend / across=2;

format date mmyyd.;

run;

by doing so, you will display the "average" percent for each month.

I am not sure that this is what you want, but I am also not sure that if you want to use the format you have another choice.

Smiley Happy

Anca.

Frequent Contributor
Posts: 104

Re: proc sgplot bar-line graph and x-axis

Thank Anca for looking at the code.  My main reason for using the mmyyd. format was to clean up the x-axis.  (It's too clutter if I keep all of the original dates.) Is there another format you recommend that wouldn't be too much for one graph but I would not have to sacrifice the original values?

Frequent Contributor
Posts: 104

Re: proc sgplot bar-line graph and x-axis

In the end I just used the fitpolicy option with rotatethin as the option.  This helps with the cluttered look.

Thanks again for your time!

Super Contributor
Posts: 543

Re: proc sgplot bar-line graph and x-axis

Excellent!

Anca.

Post a Question
Discussion Stats
  • 8 replies
  • 855 views
  • 0 likes
  • 2 in conversation