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

I have two different datasets and want to create a plot with two line graph. The X and Y axis in these two datasets are different. Is it possible to add two X and two Y axis simultaneously in one graph. I found its possible to add two Y axis in PROC SGPLOT. Is there any way to do the same for two X axis using SGPLOT or other SAS procedures?

 

data have;
input
x1 y1;
datalines;
1 45
2 53
3 66
4 77
5 58
6 48
;
data have2;
input
x2 y2;
datalines;
1 15
2 16
3 27
4 30
5 35
6 45
7 40
8 37
9 44
10 45
11 56
12 45
13 95
;


proc sgplot data=have;
series x=x1 y=y1;
run;

proc sgplot data=have2;
series x=x2 y=y2;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have1;
input
x1 y1;
datalines;
1 45
2 53
3 66
4 77
5 58
6 48
;
data have2;
input x2 y2;
datalines;
1 15
2 16
3 27
4 30
5 35
6 45
7 40
8 37
9 44
10 45
11 56
12 45
13 95
;

data want;
 merge have1 have2;
run;

proc sgplot data=want;
series x=x1 y=y1/lineattrs=(color=navy);
series x=x2 y=y2/x2axis y2axis lineattrs=(color=greenyellow);
xaxis valueattrs=(color=navy) labelattrs=(color=navy);
yaxis valueattrs=(color=navy) labelattrs=(color=navy);
x2axis valueattrs=(color=greenyellow) labelattrs=(color=greenyellow);
y2axis valueattrs=(color=greenyellow) labelattrs=(color=greenyellow);
run;

Ksharp_0-1642849948260.png

 

View solution in original post

4 REPLIES 4
ballardw
Super User

First would be to combine the data sets.

Depending upon the final desired appearance there a MANY options but this is a basic graph with both lines:

data toplot;
   set have  (in=in1)
       have2 (in=in2)
   ;
run;

proc sgplot data=toplot;
   series x=x1 y=y1;
   series x=x2 y=y2;
run;

Labels, axis controls, style settings for lines/markers might help make the result more usable.

 

 

dac_js
Quartz | Level 8

Thank you!

Ksharp
Super User
data have1;
input
x1 y1;
datalines;
1 45
2 53
3 66
4 77
5 58
6 48
;
data have2;
input x2 y2;
datalines;
1 15
2 16
3 27
4 30
5 35
6 45
7 40
8 37
9 44
10 45
11 56
12 45
13 95
;

data want;
 merge have1 have2;
run;

proc sgplot data=want;
series x=x1 y=y1/lineattrs=(color=navy);
series x=x2 y=y2/x2axis y2axis lineattrs=(color=greenyellow);
xaxis valueattrs=(color=navy) labelattrs=(color=navy);
yaxis valueattrs=(color=navy) labelattrs=(color=navy);
x2axis valueattrs=(color=greenyellow) labelattrs=(color=greenyellow);
y2axis valueattrs=(color=greenyellow) labelattrs=(color=greenyellow);
run;

Ksharp_0-1642849948260.png

 

dac_js
Quartz | Level 8
Thank you! It creates exactly what I was looking for.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 4 replies
  • 4184 views
  • 1 like
  • 3 in conversation