BookmarkSubscribeRSS Feed
sophia_SAS
Obsidian | Level 7

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!

8 REPLIES 8
AncaTilea
Pyrite | Level 9

Hi.

What happens when you try this:(please 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.

sophia_SAS
Obsidian | Level 7

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?

AncaTilea
Pyrite | Level 9

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

Thanks.

Anca,

sophia_SAS
Obsidian | Level 7

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

AncaTilea
Pyrite | Level 9

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.

sophia_SAS
Obsidian | Level 7

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?

sophia_SAS
Obsidian | Level 7

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!

AncaTilea
Pyrite | Level 9

Excellent!

Anca.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 2342 views
  • 0 likes
  • 2 in conversation