Hello all,
I'm having an issue with a graph that consists of Hbar, Scatter, and Annotations. To simplify the issue, I've created sample data as follows.
data test1;
length name run1_stop run2_stop $ 50;
name = "Bob"; team = "Team AAA"; run1 = 10; run2 = 6; run1_stop = "Need Water"; run2_stop = "Need Rest";
run;
data test2;
length name run1_stop run2_stop $ 50;
name = "Bill"; team = "Team AAA"; run1 = 8; run2 = 13; run1_stop = "Need Rest"; run2_stop = "Need Water";
run;
data final;
set test1 test2;
run;
Dataset looks like:
My goal is to create a bar graph with the following:
-X-axis = Name of runner.
-Y-axis = Distance ran.
-Annotations to show run1 vs run2.
-Markers to show why the run ended (via scatter plots). These must be consistent throughout the entire graph. Ie if a runner stopped due to "Need Rest," then the marker used should be the same for all runners and every run (run1, run2, etc).
So far I've using the following relevant lines of code with Proc Sgplot:
(1) hbarparm category = name response = run1 / discreteoffset = -0.065
(2) hbarparm category = name response = run2 / discreteoffset = 0.065
(3) scatter x = run1 y = name / group = run1_stop discreteoffset = -0.065;
(4) scatter x = run2 y = name / group = run2_stop discreteoffset = 0.065;
Below is what I'm getting:
Here is what I would like (created using MS Paint):
Is this something that can be done using SAS graphics?
Appreciate any assistance!
Appreciate the advice. I modified the data to be "longer." Below is the complete code I used to generate the dummy data. Pardon the lack of indents. Copying / Pasting from notepad seems to eliminate the indents.
Anyway, I was able to get most of what I wanted in the graph. But it's still not complete.
data test1;
length name team run1_stop run2_stop $ 50;
name = "Bob";
team = "Team AAA";
run1 = 10;
run2 = 6;
run1_stop = "Need Water";
run2_stop = "Need Rest";
run;
data test2;
length name team run1_stop run2_stop $ 50;
name = "Bill";
team = "Team AAA";
run1 = 8;
run2 = 13;
run1_stop = "Need Rest";
run2_stop = "Need Water";
run;
data final;
set test1 test2;
run;
data run1;
set final;
team = strip (team) || " - Run 1";
run_race = "Run 1";
run_label = "Run 1";
run_stop = run1_stop;
run_time = run1;
keep name team run_label run_time run_stop;
run;
data run2;
set final;
team = strip (team) || " - Run 2";
run_race = "Run 2";
run_label = "Run 2";
run_stop = run2_stop;
run_time = run2;
keep name team run_label run_time run_stop;
run;
data final2;
set run1 run2;
if run_label = "Run 1" and run_stop = "Need Water" then run1_need_water = "Need Water";
else if run_label = "Run 2" and run_stop = "Need Water" then run2_need_water = "Need Water";
if run_label = "Run 1" and run_stop = "Need Rest" then run1_need_rest = "Need Rest";
else if run_label = "Run 2" and run_stop = "Need Rest" then run2_need_rest = "Need Rest";
run;
For the graphing part, I did the following:
(1) hbarparm category = name response = run_time / group = team
(2) scatter x = run_time y = name / group = run1_need_water ... symbol=trianglefilled discreteoffset = -0.05;
(3) scatter x = run_time y = name / group = run2_need_water ... symbol=trianglefilled discreteoffset = 0.05;
(4) scatter x = run_time y = name / group = run1_need_rest ... symbol=squarefilled discreteoffset = -0.05;
(5) scatter x = run_time y = name / group = run2_need_rest ... symbol=squarefilled discreteoffset = 0.05;
The final graph looks like this:
The remaining issue is with the legend. I need the marked section to look like this:
I'll have to investigate this a little more. Appreciate any ideas or recommendations for this issue.
Quick side note:
I had to create a unique variable for water / rest for each run. Otherwise the table would have the same marker for rest and water, and the markers wouldn't line up with the individual hbar's.
I found the solution and got the final graph that I wanted. It wasn't as straightforward as expected.
If anyone would like the dummy data and the final code, please let me know. It's a bit too long to post here.
Thanks, all!
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!
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.