## how to plot multiple data on same graph

Solved
Occasional Contributor
Posts: 15

# how to plot multiple data on same graph

I want to plot two datasets on the same graph.  They are from 2 different SAS files and have two different units, so I can't use group=var to do it.

I know there is an ODS command that will lay datasets on top of one another on one graph, but I can't seem to find the code anywhere.

Accepted Solutions
Solution
‎09-05-2017 10:02 AM
Posts: 5,118

## Re: how to plot multiple data on same graph

Posted in reply to jswinford

It should go like this:

``````data obs;
input year species obs;
datalines;
1999      622             20
1999      597             32
2000      597             45
2000      596             3
2001      622             16
;

data temps;
input year temp;
datalines;
1999      8.6
2000      8.8
2001      8.9
;

data graph;
merge obs temps; by year;
run;

proc sgplot data=graph;
series x=year y=obs / group=species markers;
series x=year y=temp / y2axis;
run;``````
PG

All Replies
Super User
Posts: 12,148

## Re: how to plot multiple data on same graph

Posted in reply to jswinford

A single graphic procedure will only use one data set for the graph created. There are helper data sets that can be used to set attributes for things like line color or markers to make group variables appear consistent.

You can overlay to different graphs from the same data such as a series and scatter plot in the same SGPLOT call but they will use the same data.

Likely you best bet is to combine the two data sets with a variable to indicate which set the data comes from and possibly use that variable as a Group variable.

A bit more concrete details such as type of graph(s) and example data would help if you want more specific suggestions.

Occasional Contributor
Posts: 15

## Re: how to plot multiple data on same graph

I'm just doing a scatterplot with a line.

Dataset one example:

year       Species      Number of Observations

1999      622             20

1999      597             32

2000      597             45

2000      596             3

2001      622             16

....etc...  I have 3 species and I want 3 different lines of observations..i'm plotting years vs. # of obs.

My second dataset is simple as well:

Year       Av. Winter SST in celsius

1999      8.6

2000      8.8

2001      8.9

...etc...plotting year vs. temperature

So I want 3 species in separate lines, and one temperature line (so two different Y axis).  I'm super new to this program so bear with me.

Super User
Posts: 12,148

## Re: how to plot multiple data on same graph

Posted in reply to jswinford

Your example data has too few records and causes some appearance difficulties because of that, especiall with species 596 only appearing once. So I have faked up some additional data. Also not how the data is presented as data step.

If this were my project I would likely have a format to display a species name instead of the code unless that coding scheme is extremely well know to others or is intended for anonymity.

```data one;
input Year Species      Number;
label Number = 'Number of observations';
datalines;
1999      622             20
1999      597             32
1999      596             1
2000      622             18
2000      597             25
2000      596             3
2001      622             29
2001      597             17
2001      596             0
2002      622             22
2002      597             24
2002      596             2
2003      622             33
2003      597             31
2003      596             1
;
run;

data two;
input Year temp;
label temp = 'Mean Winter SST (C)';
datalines;
1999      8.6
2000      8.8
2001      8.9
2002      8.2
2003      8.7
;
run;

data combined;
merge  two one;
by year;
run;

proc sgplot data=combined;
series x=year y=Number/group=species
name='Species'
lineattrs=(thickness=2)
;
series x=year y=temp/ y2axis
name='temp'
lineattrs=(pattern=shortdashdot)
;
keylegend 'Species' / noborder title='Species code' ;
format year f4.0;
y2axis values=(5 to 10 by 1);
run;```

The combined set is not ideal for some uses but provides an idea.

Solution
‎09-05-2017 10:02 AM
Posts: 5,118

## Re: how to plot multiple data on same graph

Posted in reply to jswinford

It should go like this:

``````data obs;
input year species obs;
datalines;
1999      622             20
1999      597             32
2000      597             45
2000      596             3
2001      622             16
;

data temps;
input year temp;
datalines;
1999      8.6
2000      8.8
2001      8.9
;

data graph;
merge obs temps; by year;
run;

proc sgplot data=graph;
series x=year y=obs / group=species markers;
series x=year y=temp / y2axis;
run;``````
PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 4 replies
• 134 views
• 2 likes
• 3 in conversation