I got the SAS code below to add multiple texts to the figure created by sgplot. However, it does not work. Could you help me correct it? Many thanks.
/* Main data set */ data mydata; input x y; datalines; 1 10 2 20 3 30 ; run; /* Annotation data set */ data annotations; input x y text $20.; datalines; 1 15 "First Note" 2 25 "Second Note" 3 35 "Third Note" ; run; /* Plot with annotations */ proc sgplot data=mydata; scatter x=x y=y; /* Main plot */ text x=x y=y text=text / data=annotations; /* Adding annotations */ run;
/* Main data set */
data mydata;
input x y;
datalines;
1 10
2 20
3 30
;
run;
/* Annotation data set */
data annotations;
input x y text $quote20.;
datalines;
1 15 "First Note"
2 25 "Second Note"
3 35 "Third Note"
;
run;
data have;
set mydata annotations;
run;
/* Plot with annotations */
proc sgplot data=have;
scatter x=x y=y; /* Main plot */
text x=x y=y text=text /strip contributeoffsets=none; /* Adding annotations */
xaxis offsetmin=0.05 offsetmax=0.05;
run;
/* Main data set */
data mydata;
input x y;
datalines;
1 10
2 20
3 30
;
run;
/* Annotation data set */
data annotations;
input x y text $quote20.;
datalines;
1 15 "First Note"
2 25 "Second Note"
3 35 "Third Note"
;
run;
data have;
set mydata annotations;
run;
/* Plot with annotations */
proc sgplot data=have;
scatter x=x y=y; /* Main plot */
text x=x y=y text=text /strip contributeoffsets=none; /* Adding annotations */
xaxis offsetmin=0.05 offsetmax=0.05;
run;
Awesome, Many thanks!
Thanks but how to remove x and y of the annotation in the legend? I may have other variables in the data. So how to remove x and y of the annotation in the legend but keep other variables in the legend?
@SeaMoon_168 wrote:
Thanks but how to remove x and y of the annotation in the legend? I may have other variables in the data. So how to remove x and y of the annotation in the legend but keep other variables in the legend?
You need to show the exact code you are using.
Legend appearance is controlled by the Keylegend options.
If by "other variables" you mean you have multiple plots using other variables then the approach is to provide a NAME for each plot and then the Keylegend statement lists the names of the specific plot that you want included in the legend.
Example:
proc sgpanel data=sashelp.class noautolegend; panelby sex; histogram height /name='hist'; density height / type=kernel name="kernel" lineattrs=(color = red); keylegend "kernel" / title="Density Plot" titleattrs=(color = red); run;
The name for the histogram isn't required but shown as an example. The only legend that will appear is for the Density plot with the name "kernel".
"Thank you for your help with adding texts into the sgplot. But I'd also like to know how to remove x and y of the annotation in the legend? I may have other variables in the data. So how to remove x and y of the annotation in the legend but keep other variables in the legend? The original post can be accessed "
OK. You want this ?
/* Main data set */
data mydata;
input x y;
datalines;
1 10
2 20
3 30
;
run;
/* Annotation data set */
data annotations;
input x y text $quote20.;
datalines;
1 15 "First Note"
2 25 "Second Note"
3 35 "Third Note"
;
run;
data have;
set mydata annotations;
run;
/* Plot with annotations */
proc sgplot data=have;
scatter x=x y=y /name='x' ; /* Main plot */
text x=x y=y text=text /strip contributeoffsets=none ; /* Adding annotations */
xaxis offsetmin=0.05 offsetmax=0.05;
keylegend 'x';
run;
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.