Hello
I'm having troubles displaying the values in a Legend correctly - basically as I'm using a GROUP, the Legend repeats the values twice.
Here is an example of my data:
data test;
input Date $ Status $ Expected Total;
datalines;
20AUG Received 15 15
20AUG Replic . 0
19AUG Replic . 10
19AUG Received 15 145
18AUG Replic . 0
18AUG Received 15 15
17AUG Replic . 0
17AUG Received 15 15
14AUG Replic . 0
14AUG Received 15 15
13AUG Received 15 15
13AUG Replic . 0
12AUG Received 15 15
12AUG Replic . 015
11AUG Received 15 135
11AUG Replic . 20
10AUG Received 15 15
10AUG Replic . 0
07AUG Received 15 15
07AUG Replic . 0
06AUG Replic . 0
06AUG Received 15 15
;run;
proc sgplot data=WORK.TEST ;
vbar Date / name="STATUS" group=status response=TOTAL groupdisplay=stack ;
vline Date / response=EXPECTED group=status name="Expected"
lineattrs=(thickness=3 color=deeppink pattern=dash) transparency=0.1;
xaxis display=(nolabel) DISCRETEORDER=data fitpolicy=thin type=discrete ;
keylegend "STATUS" "Expected";
run;
In the legend, I'm happy with "Received" and "Replic", but I'd like to only see one dotted line, saying "Expected".
I cannot remove the GROUP as I would lose the distinction between "Received" and "Replic".
ps: In the forum I found there are solution if using EG 9 and above, but I'm using EG 7.13
Many thanks
The following should get you what you want:
data test;
input Date $ Status $ Expected Total;
datalines;
20AUG Received 15 15
20AUG Replic . 0
19AUG Replic . 10
19AUG Received 15 145
18AUG Replic . 0
18AUG Received 15 15
17AUG Replic . 0
17AUG Received 15 15
14AUG Replic . 0
14AUG Received 15 15
13AUG Received 15 15
13AUG Replic . 0
12AUG Received 15 15
12AUG Replic . 0
11AUG Received 15 135
11AUG Replic . 20
10AUG Received 15 15
10AUG Replic . 0
07AUG Received 15 15
07AUG Replic . 0
06AUG Replic . 0
06AUG Received 15 15
;run;
proc sgplot data=WORK.TEST ;
vbarbasic Date / name="STATUS" group=status response=TOTAL groupdisplay=stack
grouporder=ascending ;
highlow x=Date high=Expected low=Expected / type=bar nofill name="Expected" l
egendlabel="Expected"
lineattrs=(thickness=3 color=deeppink pattern=dash) transparency=0.1;
keylegend "STATUS" "Expected";
run;
Hope this helps!
Dan
You can remove the "Expected" from the KeyLegend
keylegend "STATUS"; /* "Expected"; */
This however would not show the dotted line with the "Expected" - as I need to show "Received" "Replic" "Expected"
thanks
Instead of adding the VLINE, add a custom LEGENDITEM entry for the line. Do something like the following:
proc sgplot data=WORK.TEST ;
vbar Date / name="STATUS" group=status response=TOTAL groupdisplay=stack ;
vline Date / response=EXPECTED group=status name="Expected"
lineattrs=(thickness=3 color=deeppink pattern=dash) transparency=0.1;
xaxis display=(nolabel) DISCRETEORDER=data fitpolicy=thin type=discrete ;
legenditem type=line name="MyLine" / lineattrs=(thickness=3 color=deeppink pattern=dash)
label="Expected";
keylegend "STATUS" "MyLine";
run;
The LEGENDITEM statement requires at least SAS v9.4m5.
Hope this helps!
Dan
Hi @DanH_sas
I did see the LEGENDITEM on one of your posts actually - unfortunately I'm on EG7.13 !
Any chance there is something working in a similar way for older versions??
thanks
What do you get in your log when you submit the following:
%put &sysvlong;
hi @DanH_sas
that's confusing!
the log shows 9.04.01M4P110916, but in the "About SAS" I see Version: 7.13
I'm quite new to SAS but I don't understand how this could be??
thanks
This means that this version of EG is using SAS v9.4m4, which means you cannot use the LEGENDITEM approach. You can try the alternate approach I proposed using REFLINEs.
Since your thresholds per group are constant, you could run a small data step on your input data to put the threshold values into two columns. Then, use two REFLINES statements that refer to each of these two columns. In the legend, refer to only one of the REFLINEs. Set LEGENDLABEL="Expected" on the referred REFLINE statement.
thanks @DanH_sas
Do you mean transposing the table as below?
however if I do so, I will lose the ability to show Received and Replicated as a stacked bar? (apologies I'm still quite new to this)
thanks
The following should get you what you want:
data test;
input Date $ Status $ Expected Total;
datalines;
20AUG Received 15 15
20AUG Replic . 0
19AUG Replic . 10
19AUG Received 15 145
18AUG Replic . 0
18AUG Received 15 15
17AUG Replic . 0
17AUG Received 15 15
14AUG Replic . 0
14AUG Received 15 15
13AUG Received 15 15
13AUG Replic . 0
12AUG Received 15 15
12AUG Replic . 0
11AUG Received 15 135
11AUG Replic . 20
10AUG Received 15 15
10AUG Replic . 0
07AUG Received 15 15
07AUG Replic . 0
06AUG Replic . 0
06AUG Received 15 15
;run;
proc sgplot data=WORK.TEST ;
vbarbasic Date / name="STATUS" group=status response=TOTAL groupdisplay=stack
grouporder=ascending ;
highlow x=Date high=Expected low=Expected / type=bar nofill name="Expected" l
egendlabel="Expected"
lineattrs=(thickness=3 color=deeppink pattern=dash) transparency=0.1;
keylegend "STATUS" "Expected";
run;
Hope this helps!
Dan
Looking at this again, I'm still not sure of the behavior of the EXPECTED values. Can these values vary per date, or are they always constant? Or, do they accumulate with your TOTAL values?
Hi @DanH_sas that's amazing, works a treat!
Never seen some of the commands you are using; very interesting many thanks.
Re the expected, it is an independent value that can change over time (so the system expects a number of files every day; if the sum of the Received and Replic will match the Expected the system is working ok, but that's not a guarantee of course).
again may thanks
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.