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:
Here is my code:
Here is what I need:
Any assistance would be appreciated.
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;
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.
The data set has aggregate data. The Keypoint is character and the defined length of 8 characters. See attached data.
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.
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.