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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
/* 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;

Ksharp_0-1733361000543.png

 

View solution in original post

5 REPLIES 5
Ksharp
Super User
/* 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;

Ksharp_0-1733361000543.png

 

SeaMoon_168
Quartz | Level 8

Awesome, Many thanks!

SeaMoon_168
Quartz | Level 8

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?

ballardw
Super User

@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".

Ksharp
Super User
"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;

Ksharp_0-1733446577753.png

 

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
  • 5 replies
  • 1086 views
  • 0 likes
  • 3 in conversation