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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

jswinford
Obsidian | Level 7

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.

 

ballardw
Super User

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.

 

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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