Hi Everyone,
I am creating a series plot/graph using PROC SGPLOT
series x=value y=curve;
I want to show different equation values on different pages in legend.
(I do not want to use inset statement as in the legend the equation should appear beside the series line)
I am making different pages for each different value of parameters hence the equation of curve changes.
Any leads will be appreciated.
Annotate or TEXT statements, assuming you've already explored the KEYLEGEND statement.
Could you please give me an example for ANNOTATE in PROC SGPLOT. I am not how will this work, will it change the value of the equation every time the PAGE of PLOT changes.
Hello, can you use the CURVELABEL option? i.e.
proc sgplot data=sashelp.stocks
(where=(date >= "01jan2000"d and stock = "IBM"));
title "Stock Trend";
series x=date y=close / curvelabel = "y=x";
run;
title;
I have already tried this option but the equation is too large that the plot does not look good.
Thank you for the suggestion.
So I need the legend which is right below the series plot where the "Equation" in legend changes every page. Please Note: I have already tried many options such as : TEXT, INSET,KEYLEGEND etc . I am also aware that the text can be macrotised but I just need an idea for the Different legend which changes on each page.
any information or any leads will be appreciated. Than you.
I just made a group variable with the text you wanted for the equation and then used that as the GROUP statement in the SERIES plot. I can then add that to the legend to get it how you're showing. By then also including that variable in the BY statement I can get one equation per page. You can use the same technique to make one graph at a time as well if you use the WHERE statement I commented out to select one at a time and it'll look the same (but then you can customize individual axes/colors/etc.).
data test;
length formula $100.;
formula='Equation: x**2';
do x=0 to 10;
y=x**2;output;
end;
formula='Equation: 2x-5';
do x=0 to 10;
y=2*x-5;output;
end;
formula='Equation: log(x)';
do x=0 to 10;
if x=0 then y=log(0.0001);
else y=log(x);
output;
end;
run;
options nobyline;
ods pdf file='~/ibm/test.pdf' notoc bookmarkgen=no;
proc sgplot data=test;
by notsorted formula;
series x=x y=y / group=formula smoothconnect;
keylegend / location=outside position=bottom title='';
xaxis min=0;
yaxis min=0;
*where formula='Equation: x**2';
run;
ods pdf close;
Here is some potential code that you can use.
I created a macro with two arguments, you just need to add the graph, i.e. the parameter and also the equation.
data stocks;
set sashelp.stocks(in=a) sashelp.stocks(in=b);
if a then do;
graph = "Graph 1";
end;
if b then do;
close = close + 100 + (close *2);
graph = "Graph 2";
end;
run;
%macro legend(equation=, graph=);
title1 #byval1;
proc sgplot data=stocks(where=(date >= "01jan2000"d and stock = "IBM"));
by graph;
where graph = "&graph";
series x=date y=close / name = "leg" legendlabel=" ";
keylegend "leg" / title = "&equation" type=line;
run;
%mend;
%legend(equation=%str(Y=X), graph=%str(Graph 1));
%legend(equation=%str(Y=100 + 2X), graph=%str(Graph 2));
You're welcome. Do you know you can also use CALL EXECUTE to do your macro calls, that way, you don't have to do it yourself 100 times and you can use your data to do it. Another alternative would just be to use the BY variable processing which Jeff showed.
This code is incase you are interested in knowing how to do the CALL EXECUTE example...
data stocks;
set sashelp.stocks(in=a) sashelp.stocks(in=b);
length equation $20;
if a then do;
graph = "Graph 1";
equation = "Y=X";
end;
if b then do;
close = close + 100 + (close *2);
graph = "Graph 2";
equation = "100 + 2X";
end;
run;
proc sql;
create table equations as
select distinct equation, graph
from stocks
order by graph;
quit;
%macro legend(equation=, graph=);
title1 #byval1;
proc sgplot data=stocks(where=(date >= "01jan2000"d and stock = "IBM"));
by graph;
where graph = "&graph";
series x=date y=close / name = "leg" legendlabel=" ";
keylegend "leg" / title = "&equation" type=line;
run;
%mend;
options nobyline;
data _null_;
set equations;
call execute('%legend(equation=%str('||equation||'), graph=%str('||graph||'))');
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.