BookmarkSubscribeRSS Feed
Emma8
Quartz | Level 8

Hi. Can anyone help with the below problem?

I would like to label each line as Q1, median, and Q3 at the end of the lines. The below code does not show the text label.

I attached the data.

Thank you.

 

 

 

/* Create the Annotate data set LINETEXT */
data linetext (drop=var26: time );
length function text $ 20 style $ 25;
retain function 'label' xsys ysys '2' position '2' color 'black' size 1.5;
set DATA end=last;
style = "'Albany AMT/bold'";
if last then do;
x=Time;
y= left(put(var26r_Q1,percent12.0)); text='Q1'; output;
y= left(put(var26r_Median,percent12.0)); text='Median'; output;
y= left(put(var26r_Q3,percent12.0)); text='Q2'; output;
end;
run;

 

proc gplot data=DATA;
plot (var26r_Q1 var26r_Median var26r_Q3 )*Time / overlay frame
haxis=axis1 vaxis=axis2
annotate=linetext ;
axis1 minor=none offset=(1,8)pct label=('Year and Quarter');
axis2 order=(0 to 1 by 0.2) offset=(2,1)pct
label=('CRC' justify=right '(in percentage)');
/* Choose the interpolation method and assign symbol characteristics */
symbol1 interpol=line width=2 color='#99d8c9';
symbol2 interpol=line width=2 color='#2ca25f';
symbol3 interpol=line width=2 color='#99d8c9';
format var26r_Q1 var26r_Median var26r_Q3 percent12.0;
run;
quit;

6 REPLIES 6
ballardw
Super User

@Emma8 wrote:

Hi. Can anyone help with the below problem?

I would like to label each line as Q1, median, and Q3 at the end of the lines. The below code does not show the text label.

I attached the data.

Thank you.

 

 

 

/* Create the Annotate data set LINETEXT */
data linetext (drop=var26: time );
length function text $ 20 style $ 25;
retain function 'label' xsys ysys '2' position '2' color 'black' size 1.5;
set DATA end=last;
style = "'Albany AMT/bold'";
if last then do;
x=Time;
y= left(put(var26r_Q1,percent12.0)); text='Q1'; output;
y= left(put(var26r_Median,percent12.0)); text='Median'; output;
y= left(put(var26r_Q3,percent12.0)); text='Q2'; output;
end;
run;

 

proc gplot data=DATA;
plot (var26r_Q1 var26r_Median var26r_Q3 )*Time / overlay frame
haxis=axis1 vaxis=axis2
annotate=linetext ;
axis1 minor=none offset=(1,8)pct label=('Year and Quarter');
axis2 order=(0 to 1 by 0.2) offset=(2,1)pct
label=('CRC' justify=right '(in percentage)');
/* Choose the interpolation method and assign symbol characteristics */
symbol1 interpol=line width=2 color='#99d8c9';
symbol2 interpol=line width=2 color='#2ca25f';
symbol3 interpol=line width=2 color='#99d8c9';
format var26r_Q1 var26r_Median var26r_Q3 percent12.0;
run;
quit;


Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Please post data as a data step. Your xls file did not have numeric values for the x axis so plots don't want to work correctly.

 

You really should consider using the newer plots SGPLOT as there are many more options available and the output will generally work better with the ODS files created:

 

Consider:

proc sgplot data=data;
   series x=time y= var26r_Q1     / lineattrs=( color='#99d8c9') curvelabel='Q1';
   series x=time y= var26r_Median / lineattrs=( color='#2ca25f') curvelabel='Median';
   series x=time y= var26r_Q3     / lineattrs=( color='#99d8c9') curvelabel='Q3';
run;

options involved with the INTERPOL symbol options are mostly involved with LINEATTRS for line width, color and type.

 

Emma8
Quartz | Level 8

Thank you.

Sorry, it should be formatted by percent12.0.

See attached.

 

 

ballardw
Super User

@Emma8 wrote:

Thank you.

Sorry, it should be formatted by percent12.0.

See attached.

 

 


Then add the format statement.

A

Emma8
Quartz | Level 8

Can you use the annotate in PROC GPLOT?

 Thank you.

ballardw
Super User

@Emma8 wrote:

Can you use the annotate in PROC GPLOT?

 Thank you.


I can and basically choose not to any more as the basic graph functionality is limited and showing multiple types of plots together is usually difficult plus often impossible.

 

Currently to have access to Proc Gplot you need an additional license to SAS/Graph while the statistical graphics procedures Sgplot, Sgpanel and such are part of BASE SAS. Also Proc SGPLOT supports all the plot types of GPLOT, GChart and several more.

 

One is a TEXTPLOT where you can have X, Y coordinates and a variable holding text. So if you have some records at the end of data set you can add text at any coordinates without having to go through the annotate facility.

 

 

Without an actual SAS data set to attempt to graph I wouldn't even try. XLS is not a data set. I have to make choices to create a data set and my choices likely do not match yours and may not be able to plot it at all.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

GraphGuy
Meteorite | Level 14

Hi Emma,

 

You had everything very close to being what you needed!

 

Since the x value in the plot ('year_qtr') are character, you'll need to use xc, rather than x, in your annotate dataset.

And since your y values in the plot are numeric, you'll need to use their raw/numeric values as y in your annotate dataset (rather than applying the format, and saving them as character).

 

I've made those changes, and added a few little "finishing touches", and included the graph below:

 

 

proc import out=my_data datafile='data.xls' dbms=xls;
run;

 

data anno_text (drop=var26: time); set my_data end=last;
length text $50;
xsys='2'; ysys='2'; hsys='3'; when='a';
function='label'; style='Albany AMT/bold';
color='gray33'; position='>'; size=2.0;
if last then do;
   xc=Time;
   y=var26r_Q1; text='a0'x||'Q1'; output;
   y=var26r_Median; text='a0'x||'Median'; output;
   y=var26r_Q3; text='a0'x||'Q2'; output;
   end;
run;

 

goptions gunit=pct htitle=5 htext=3.0 ftitle="albany amt/bold" ftext="albany amt";
goptions ctext=gray33;

axis1 minor=none offset=(1,8)pct label=('Year and Quarter')
value=(angle=90) label=none;

axis2 order=(0 to 1 by 0.2) offset=(0,0) minor=none
label=('CRC' justify=right '(in percentage)');

symbol1 interpol=line width=2 color='#99d8c9';
symbol2 interpol=line width=2 color='#2ca25f';
symbol3 interpol=line width=2 color='#99d8c9';

 

proc gplot data=my_data;
format var26r_Q1 var26r_Median var26r_Q3 percent12.0;
plot (var26r_Q1 var26r_Median var26r_Q3 )*Time / overlay frame
haxis=axis1 vaxis=axis2
annotate=anno_text;
run;

 

emmaplot.png

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1641 views
  • 1 like
  • 3 in conversation