Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Graphics
- /
- Multiple legends PROC SGPLOT

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 08-06-2021 01:45 PM
(4185 views)

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.

13 REPLIES 13

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Annotate or TEXT statements, assuming you've already explored the KEYLEGEND statement.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Where does the equation come from? You can have SAS generate code via CALL EXECUTE, Macros or DOSUBL.

I cannot provide a dynamic solution for unseen data structures.

I cannot provide a dynamic solution for unseen data structures.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi, I am aware of the macros and another ways where distinct values can be stored. But I just wanted more information/example on "ANNOTATE" for LEGEND which will change for each page.(I did a lot of searches on the topic for ANNOTATE for LEGEND purposes but did not find anything which was useful for me) Thanks ..!!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I have already tried this option but the equation is too large that the plot does not look good.

Thank you for the suggestion.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Is it possible to give an example of exactly what you want? Even temporarily using a textbox in paint to show it? I'm having a hard time picturing it but it sounds like something I can make.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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));
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi ,

Thank you for the suggestion but I have total 100 PARAMETERS ie. 100 different equation and 100 pages so I have macrotised PROC SGPLOt rather than calling "%legend" macro 100 times ,though I am aware this can be one of the solution but I am looking for more efficient way.

Thank you for the suggestion but I have total 100 PARAMETERS ie. 100 different equation and 100 pages so I have macrotised PROC SGPLOt rather than calling "%legend" macro 100 times ,though I am aware this can be one of the solution but I am looking for more efficient way.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 25. Read more here about **why** you should contribute and **what is in it** for you!

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.