BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Giovani
Obsidian | Level 7

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: excell.PNG

But in SAS it is not possible to read the X Axis Capture.PNG

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!

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

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

View solution in original post

10 REPLIES 10
ballardw
Super User

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.

 

 

DanH_sas
SAS Super FREQ

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

Giovani
Obsidian | Level 7

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?

Capture.PNG

ballardw
Super User

@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.

Giovani
Obsidian | Level 7

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.

DanH_sas
SAS Super FREQ

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.

SuryaKiran
Meteorite | Level 14

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;
Thanks,
Suryakiran
DanH_sas
SAS Super FREQ

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 

Giovani
Obsidian | Level 7

Fantastic!

 

Thank very much!

Working perfectlly

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 5645 views
  • 4 likes
  • 4 in conversation