Hi SAS experts,
I am trying to build a series plot in sgplot using X axis as year and week.
when I build the graph in excel it returns a X axis label like this:
But in SAS it is not possible to read the X Axis
The data is organized like this:
Data Cars;
input
year $ week $ Small_Size $ Mid_Size $ Large_Size $ Van $
;
cards;
2018 1 10 15 23 16
2018 2 12 11 28 18
2018 3 15 19 17 11
;
run;
Data Cars; set cars;
YearWeek=catx(' ',Year,Week);
run;
Data Cars; set cars;
format YearWeek $7.;
run;
And the SAS codes for the graph that I am using is this:
ods graphics on /
width=10 in
outputfmt=gif
imagemap=on
imagename="Cars"
border=off;
PROC SGPLOT DATA = Cars;
SERIES X = YearWeek Y = Small_Size;
SERIES X = YearWeek Y = Mid_Size;
SERIES X = YearWeek Y = Large_Size;
SERIES X = YearWeek Y = Van;
XAXIS TYPE = DISCRETE GRID;
YAXIS LABEL = 'Number of cars' GRID VALUES = (5 TO 40 BY 1);
TITLE 'Sales by type';
run;
Any suggestion how I could control the X axis to look some how like the one I am getting in excel?
Thank very much!
There are two changes I would recommend:
1. Do not input YEAR and WEEK as character strings. Input them as numbers.
Change:
input year $ week $ Small_Size $ Mid_Size $ Large_Size $ Van $;
to:
input year week Small_Size $ Mid_Size $ Large_Size $ Van $;
That way, the axis is not forced to discrete, and the axis will thin out naturally for linear numbers. If you really want to keep the axis discrete, keep your TYPE=DISCRETE and add FITPOLICY=THIN. THE best way to represent this information is using a time axis, but that would require you to turn your year/week values into SAS datatime values.
2. Instead of using SGPLOT, try using SGPANEL to create the layout you want for this case:
proc sgpanel data=cars;
panelby year / noborder layout=COLUMNLATTICE uniscale=row
onepanel colheaderpos=bottom novarname;
SERIES X = week Y = Small_Size;
SERIES X = week Y = Mid_Size;
SERIES X = week Y = Large_Size;
SERIES X = week Y = Van;
colaxis GRID;
rowaxis LABEL = 'Number of cars' GRID VALUES = (5 TO 40 BY 1);
TITLE 'Sales by type';
run;
Hope this helps!
Dan
Remove discrete from the xaxis to show fewer tick marks and values.
Notice that your EXCEL example does not attempt to display each value of week.
There are two changes I would recommend:
1. Do not input YEAR and WEEK as character strings. Input them as numbers.
Change:
input year $ week $ Small_Size $ Mid_Size $ Large_Size $ Van $;
to:
input year week Small_Size $ Mid_Size $ Large_Size $ Van $;
That way, the axis is not forced to discrete, and the axis will thin out naturally for linear numbers. If you really want to keep the axis discrete, keep your TYPE=DISCRETE and add FITPOLICY=THIN. THE best way to represent this information is using a time axis, but that would require you to turn your year/week values into SAS datatime values.
2. Instead of using SGPLOT, try using SGPANEL to create the layout you want for this case:
proc sgpanel data=cars;
panelby year / noborder layout=COLUMNLATTICE uniscale=row
onepanel colheaderpos=bottom novarname;
SERIES X = week Y = Small_Size;
SERIES X = week Y = Mid_Size;
SERIES X = week Y = Large_Size;
SERIES X = week Y = Van;
colaxis GRID;
rowaxis LABEL = 'Number of cars' GRID VALUES = (5 TO 40 BY 1);
TITLE 'Sales by type';
run;
Hope this helps!
Dan
Dear Dan;
THANK VERY MUCH!
It worked great. Only one small DETAIL to be perfect.
When change from one year to another there is gap in the graph. Any idea how this could be avoided making the lines to connect each other?
@Giovani wrote:
Dear Dan;
THANK VERY MUCH!
It worked great. Only one small DETAIL to be perfect.
When change from one year to another there is gap in the graph. Any idea how this could be avoided making the lines to connect each other?
Another reason to use an actual DATE value. That would be continuous. Also you can create or apply a format shows tick values with a possibly more intuitive appearance.
Hi Ballardw,
Removing DISCRETE didn`t correct the issue.
Actually I don`t have the a specific date for the information. I only have the year value and a value for week.
You can try setting SPACING=0 or SPACING=1 on the PANELBY statement to help with this gap, but ballardw is correct. If you converted year/week to a true datetime value, you can use a time axis in SGPLOT that will give you the look you want (year split from weeks) without the gap.
Give this a try:
Data Cars;
input
year $ week $ Small_Size $ Mid_Size $ Large_Size $ Van $;
cards;
2018 1 10 15 23 16
2018 2 12 11 28 18
2018 3 15 19 17 11
;
run;
Data Cars;
FORMAT YearWeek WEEKW5.;
set cars;
YearWeek=INPUT(CATS(substr(YEAR,3,2),"W",put(input(week,2.),z2.)),weekw5.);
run;
ods graphics on /
width=10 in
outputfmt=gif
imagemap=on
imagename="Cars"
border=off;
PROC SGPLOT DATA = Cars;
SERIES X = YearWeek Y = Small_Size;
SERIES X = YearWeek Y = Mid_Size;
SERIES X = YearWeek Y = Large_Size;
SERIES X = YearWeek Y = Van;
/* XAXIS TYPE = DISCRETE GRID;*/
YAXIS LABEL = 'Number of cars' GRID VALUES = (5 TO 40 BY 1);
TITLE 'Sales by type';
run;
SuryaKiran,
Thank very much!
If you're going to use the SGPANEL approach, another way to get rid of the gap between plots is to get rid of the axis offsets:
colaxis offsetmin=0 offsetmax=0;
There is the possibility of tick value collisions, but you can give it a try and see if it works for you. Also, make sure you have SPACING=0 on the PANELBY statement.
Thanks!
Dan
Fantastic!
Thank very much!
Working perfectlly
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.