Solved
Contributor
Posts: 23

# SGPLOT - Series grouped by year

[ Edited ]

Hi

I have a monthly time series from January 1960 to December 2017.

I know to plot the whole series with SGPLOT.

PROC SGPLOT DATA=XYZ;

series x=date  y=ABC;

xaxis values=('1jan60'd to '1dec17'd BY month);

RUN;

However, i would like to plot 4 sub-series on the same plot:

series1 only year 1960

series2 only year 1970

series3 only year 1980

series4 only year 1990

Thanks

Accepted Solutions
Solution
‎12-14-2017 03:14 PM
Super User
Posts: 22,874

## Re: SGPLOT - Series grouped by year

Welcome to the SAS forums. Note that I've moved your post to the graphics board as it's more related.

Regarding your question, I would suggest the following approach:

1. Use a WHERE clause to filter the data.

2. Create new variables month & year that will reflect the month and year

3. Same code with the WHERE filter and then use GROUP=YEAR and month as date instead.

You'll probably need to customize this some more but hopefully it gets you started.

``````data temp;
set xyz;

month=month(date);
mon_name = put(date, monname3.);
year = year(date);

run;

proc sgplot data=temp;

where year in (1960, 1970, 1980, 1990);

series  x= month y=abc / group=year;

run;``````

All Replies
Solution
‎12-14-2017 03:14 PM
Super User
Posts: 22,874

## Re: SGPLOT - Series grouped by year

Welcome to the SAS forums. Note that I've moved your post to the graphics board as it's more related.

Regarding your question, I would suggest the following approach:

1. Use a WHERE clause to filter the data.

2. Create new variables month & year that will reflect the month and year

3. Same code with the WHERE filter and then use GROUP=YEAR and month as date instead.

You'll probably need to customize this some more but hopefully it gets you started.

``````data temp;
set xyz;

month=month(date);
mon_name = put(date, monname3.);
year = year(date);

run;

proc sgplot data=temp;

where year in (1960, 1970, 1980, 1990);

series  x= month y=abc / group=year;

run;``````
Contributor
Posts: 23

## Re: SGPLOT - Series grouped by year

Thanks

Nice solution and very fast answer.

I just add two options: markers and xaxis values.

DATA temp;

SET OUT;

month=month(date);

mon_name = put(date,monname3.);

year = year(date);

RUN;

PROC SGPLOT DATA=temp;

WHERE year in (1960, 1970, 1980, 1990);

series x=month y=unemployment_A1 / markers group=year;

xaxis values=(1 TO 12);

RUN;

Community Manager
Posts: 3,364

## Re: SGPLOT - Series grouped by year

Grouped by year or grouped by decade?  Either way, you need a grouping variable, which you can derive from DATE in a DATA step.

If grouping by year, you can simply "clone" DATE and apply a YEAR. format.  But if it's by decade, you'll need to do a little calculation to create a value for each decade and store that in a new variable.

Then you can use the GROUP= option on your SERIES statement.

Contributor
Posts: 23