BookmarkSubscribeRSS Feed
Whitlea
Obsidian | Level 7

Is there a way I can only show the data label for one series when the data point is the same value for both series? 

For example, if both series have a value of 0% for the same month, I only want to show the 0% once.

 

Whitlea_3-1735501251474.png

Here is my proc sgplot code:

proc sgplot data=final noborder ;  

series x=date y=&y1/ datalabel=&y1 DATALABELATTRS=(family='Roboto' SIZE=10pt weight=bold color=black)DATALABELPOS=top
MARKERS MARKERATTRS=(SYMBOL=CircleFilled color=red size=8) LINEATTRS = (color=red pattern=solid THICKNESS=3) legendlabel= "A" ;

series x=date y=&y2 / datalabel=&y2 DATALABELATTRS=(family='Roboto' SIZE=10pt color=black)DATALABELPOS=bottom
MARKERS MARKERATTRS=(SYMBOL=CircleFilled color=blue size=8) LINEATTRS = (color=blue pattern=solid THICKNESS=3) legendlabel= "B";

yaxis  grid VALUES = (&values) display=(noline noticks) valueATTRS=(family='Roboto' size=10pt)
label=" " LABELATTRS=(family='Roboto' size=10pt);

XAXIS TYPE = LINEAR  display=( noline noticks)label=" "
LABELATTRS=(family='Roboto' size=10pt ) valueATTRS=(family='Roboto' size=10pt ) ;


keylegend "A" / noborder location=outside position=bottom   valueATTRS=(family='Roboto' size=10pt weight=bold)  ;

keylegend "B" / noborder location=outside position=bottom  valueATTRS=(family='Roboto' size=10pt);
title &TITLE1 ;

run;

 Thank you!

2 REPLIES 2
quickbluefish
Barite | Level 11

The reason this is challenging is because you're trying to use the same variable for the datalabel and the Y position. One thing you could do is disentangle these from each other by making a separate variable for these two - e.g., &Y2 and &Y2label  (writing it this way because you seem to be specifying these as macro variables in your code).  Then, depending on how your data are set up, you could set Y<n>label to missing for any situation where, for a given value of X, you have duplicate values of Y*.  

 

However, I think you could solve this more directly by only having ONE series statement and using the GROUP= argument to differentiate the series plots.   That's the whole intent of GROUP option.   That said, there are situations where doing it this way is limiting in some way.  Let us know whether using GROUP helps.  

 

Note that doing it with GROUP means that you only have one variable for X, one for Y and one for datalabel (though I think in this case you could just use Y for the datalabel).  The key difference vs. what you're currently doing is that the data for the different series plots (in your case, 2) are on *separate rows* of your input dataset, and you add some variable (let's just call it GRP) that delineates the data for the different groups:  

data for_plot;
infile cards dsd truncover firstobs=1 dlm=',';
length X Y 8 GRP $1;
input X Y GRP;
cards;
1,3.7,A
1,4.5,B
2,1.8,A
2,0.0,B
3,5.1,A
3,3.6,B
;
run;

proc sgplot data=for_plot;
series x=X y=Y / group=GRP datalabel=Y;
run;

 

quickbluefish
Barite | Level 11

OK, I lied - turns out the GROUP= option doesn't appear to solve your original issue, which is to avoid showing the duplicate Y* values in the plot labels.  Try this instead:

 

data for_plot;
infile cards dsd truncover firstobs=1 dlm=',';
length X Y1 Y2 8;
input X Y1 Y2;
cards;
1,3.8,4.2
2,0,0
3,2.6,3.7
4,3.1,5.6
;
run;

data for_plot;
set for_plot;
Y1lab=Y1; 
Y2lab=Y2;
if Y1=Y2 then Y2lab=.;
run;

proc sgplot data=for_plot;
series x=X y=Y1 / datalabel=Y1lab;
series x=X y=Y2 / datalabel=Y2lab;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 637 views
  • 2 likes
  • 2 in conversation