Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Graphics
- /
- how plot 2 variables on the same x axis controling the X axis label in...

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 06-15-2018 10:21 AM
(4718 views)

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!

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

10 REPLIES 10

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Suryakiran

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

SuryaKiran,

Thank very much!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Fantastic!

Thank very much!

Working perfectlly

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.