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

Hello,

 

I am working on a line graph and want the first and last data labels to be on the outside ends of the line. I have added a datalabel character variable (KEYPOINT) to my dataset. I have also added a datalabel position variable (LABEL_POSITION) that indicates right for the 2022 datalabel and left for the 2018 datalabel.  Here is my output:

Lewinda_1-1722361561450.png

 

 

Here is my code:

PROC SGPLOT DATA= WORK.INPUT NOAUTOLEGEND NOBORDER;
SERIES X= YEAR Y= RATE /
  LINEATTRS=(COLOR= BLACK THICKNESS = 2);
 
TEXT X= YEAR Y=RATE TEXT=KEYPOINT / 
  TEXTATTRS= (SIZE= 9PT) 
  POSITION=LABEL_POSITION
  STRIP
  CONTRIBUTEOFFSETS=(XMIN XMAX);
 
xaxis display=(NOLABEL) valueattrs= (family= arial size= 9pt) type=discrete;
yaxis display= NONE valueattrs=(family= arial size= 9pt);
 
RUN;
 

Here is what I need:

Lewinda_0-1722361371484.png

Any assistance would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards truncover expandtabs;
input DEATHYR	RATE	KEYPOINT	LABEL_POSITION $;
cards;
2013	7.17	    7.17	LEFT
2014	6.00		
2015	5.00		
2016	6.00		
2017	7.00		
2018	6.00		
2019	6.00		
2020	7.00		
2021	7.00		
2022	6.31	    6.31	RIGHT
;

proc sql noprint;
select RATE into :max trimmed from have having DEATHYR=max(DEATHYR);
select RATE into :min trimmed from have having DEATHYR=min(DEATHYR);
quit;
proc sgplot data=have;
series x=DEATHYR y=RATE/curvelabel="&max." curvelabelpos=max curvelabelattrs=(color=black) LINEATTRS=(COLOR= BLACK THICKNESS = 2);
series x=DEATHYR y=RATE/curvelabel="&min." curvelabelpos=min curvelabelattrs=(color=black) lineattrs=(thickness=0);
yaxis offsetmin=0.05 offsetmax=0.05;
run;

Ksharp_0-1722388010609.png

 

View solution in original post

4 REPLIES 4
ballardw
Super User

Easiest might be change the X coordinate values for the text plot. You don't show what your data set looks like. If you appended a couple observations to add the Text plot values that might be easiest.

 

Can you share your Work.input data set? May have different approaches if Keypoint is numeric vs character and the defined length of character values.

Lewinda
Fluorite | Level 6

The data set has aggregate data. The Keypoint is character and the defined length of 8 characters. See attached data.

ballardw
Super User

This may get you started: Please note the data step to provide data.

A separate Y coordinate for the values of two label values you want prevents them from getting connected to the series. Changed X coordinates to provide some space and X axis options, removing discrete and replacing with a list of values for the tick marks (I am assuming that was the reason discrete was used) and adding OFFSET values to provide bit of space from the axis tick values to display in the graph area.

 

data work.input;
  input year rate keypoint $ y2;
datalines;
2012.5 .	7.17  7.17
2013	7.17	. . 
2014	6.00	. . 
2015	5.00	. . 
2016	6.00	. . 
2017	7.00	. . 
2018	6.00	. . 
2019	6.00	. . 
2020	7.00	. . 
2021	7.00	. . 
2022	6.31	. . 
2022.5  .	6.31 6.31
;

PROC SGPLOT DATA= WORK.INPUT NOAUTOLEGEND NOBORDER;
   SERIES X= YEAR Y= RATE /
     LINEATTRS=(COLOR= BLACK THICKNESS = 2)
   ;
 
   TEXT X= YEAR Y=y2 text=keypoint / 
     TEXTATTRS= (SIZE= 9PT) 
   ;
   xaxis display=(NOLABEL) valueattrs= (family= arial size= 9pt) 
        values=(2013 to 2022 by 1) offsetmin=0.1  offsetmax=0.1
   ;
   yaxis display= NONE valueattrs=(family= arial size= 9pt);
run;

The offsets and changed values for the X coordinate may need adjusting depending on size and/or display destination.

 

If you generated the series plot points from a procedure like proc means you could easily create two rows of data with shifted x coordinate text and a second Y variable as a data step and append to the series data before plotting.

 

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values. A data step, especially for small example data sets, is very much preferred as we do not have guess about variable type, name, formats or labels that might be assigned.

 

It is good idea to post code in either a text or code box opened on the forum with the </> or "running man" icons above the main message window. The software this forum uses will reformat pasted text changing white space characters and may result with code that will not run because of not visible formatting characters when copied from the message window and pasted into your program editor.

 

I am sure this is not the only way to get the desired appearance.

 

Ksharp
Super User
data have;
infile cards truncover expandtabs;
input DEATHYR	RATE	KEYPOINT	LABEL_POSITION $;
cards;
2013	7.17	    7.17	LEFT
2014	6.00		
2015	5.00		
2016	6.00		
2017	7.00		
2018	6.00		
2019	6.00		
2020	7.00		
2021	7.00		
2022	6.31	    6.31	RIGHT
;

proc sql noprint;
select RATE into :max trimmed from have having DEATHYR=max(DEATHYR);
select RATE into :min trimmed from have having DEATHYR=min(DEATHYR);
quit;
proc sgplot data=have;
series x=DEATHYR y=RATE/curvelabel="&max." curvelabelpos=max curvelabelattrs=(color=black) LINEATTRS=(COLOR= BLACK THICKNESS = 2);
series x=DEATHYR y=RATE/curvelabel="&min." curvelabelpos=min curvelabelattrs=(color=black) lineattrs=(thickness=0);
yaxis offsetmin=0.05 offsetmax=0.05;
run;

Ksharp_0-1722388010609.png

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 578 views
  • 2 likes
  • 3 in conversation