I get 4 correlated lines, with SGPLOT(picture below).
Y2AXIS allows to plot 2-Y-Axis; It helps.
BUT BETTER is with 4-Y-Axis... HOW TO DO THAT?! Thanks,
/*
Why not use multi panel to polt these four series?
*/
proc sort data= sashelp.stocks out=have;
by date;
run;
data want;
merge have(keep=stock date close rename=(close=IBM) where=(stock='IBM') in=ina)
have(keep=stock date close rename=(close=Intel) where=(stock='Intel') in=inb)
have(keep=stock date close rename=(close=Microsoft) where=(stock='Microsoft')) ;
by date;
if ina then IBM=IBM+10000;
if inb then Intel=Intel+1000;
drop stock;
run;
proc sgscatter data=want;
compare x=date y=(ibm intel microsoft)/markerattrs=(size=0) join spacing=4;
run;
/*
Why not use multi panel to polt these four series?
*/
proc sort data= sashelp.stocks out=have;
by date;
run;
data want;
merge have(keep=stock date close rename=(close=IBM) where=(stock='IBM') in=ina)
have(keep=stock date close rename=(close=Intel) where=(stock='Intel') in=inb)
have(keep=stock date close rename=(close=Microsoft) where=(stock='Microsoft')) ;
by date;
if ina then IBM=IBM+10000;
if inb then Intel=Intel+1000;
drop stock;
run;
proc sgscatter data=want;
compare x=date y=(ibm intel microsoft)/markerattrs=(size=0) join spacing=4;
run;
I like KSharp's panel alternative. You can use PROC TRANSPOSE to convert the data from wide to long, then use PROC SGPANEL. Here is the method on the data in the blog post:
data torque;
input Torque RPM Amps Eff;
RPM=RPM/50;
Amps=Amps / 5;
datalines;
10 4000 95 80
20 3300 130 84
30 2800 160 86.5
40 2450 200 87.5
50 2200 230 87.6
60 2000 255 87.7
70 1840 285 87.5
80 1700 320 87
90 1600 345 86.2
100 1510 375 85.7
110 1470 395 85
120 1400 420 84
130 1400 440 83
140 1380 460 82
150 1380 480 81
;
/* Transpose the variables from wide to long format:
https://blogs.sas.com/content/iml/2011/01/31/reshaping-data-from-wide-to-long-format.html
*/
proc transpose data=torque
out=long(rename=(Col1=Value)) name=VarName;
by Torque;
var RPM Amps Eff; /* make a row for these variables */
run;
proc sgpanel data=long;
panelby VarName / columns=1 onepanel novarname;
series x=Torque y=Value;
rowaxis grid;
colaxis grid;
run;
/*Here is an example*/
/*make a sample data*/
data have;
call streaminit(123);
do x=1 to 100;
a=rand('integer',1000,2000); a=1456;
b=rand('integer',40000,80000); b=63457;
c=rand('integer',100000,150000); c=140932;
d=rand('integer',28000,38000);
output;
end;
run;
data axis;
do a=1000 to 2000 by 100;
label_a=a;
label_b=40000+ (80000-40000) * (a-1000)/(2000-1000);
label_c=100000+ (150000-100000) * (a-1000)/(2000-1000);
output;
end;
format label_a label_b label_c best10.;
run;
proc sort data=have;by a;run;
data want;
merge have axis;
by a;
_b=1000+ (2000-1000)* (b-40000)/(80000-40000) ;
_c=1000+ (2000-1000)* (c-100000)/(150000-100000) ;
run;
proc sort data=want;by x;run;
options missing=' ';
proc sgplot data=want;
series x=x y=a/lineattrs=(color=red);
series x=x y=_b/lineattrs=(color=blue);
series x=x y=_c/lineattrs=(color=green) ;
series x=x y=d/lineattrs=(color=purple) y2axis;
yaxis values=(1000 to 2000 by 100) display=(novalues nolabel) ;
*labelpos=top labelattrs=(color=red size=10) valueattrs=(color=red size=10);
yaxistable label_c/position=left labelattrs=(color=green size=10) valueattrs=(color=green size=10) ;
yaxistable label_b/position=left labelattrs=(color=blue size=10) valueattrs=(color=blue size=10);
yaxistable label_a/position=left labelattrs=(color=red size=10) valueattrs=(color=red size=10);
y2axis labelpos=top labelattrs=(color=purple size=10) valueattrs=(color=purple size=10);
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.