BookmarkSubscribeRSS Feed
snapbolt
Calcite | Level 5

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:

 

dd.PNG

 

 

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:

 

p1.PNG

 

 

Here is what I would like (created using MS Paint):

 

p2.PNG

 

 

 

Is this something that can be done using SAS graphics?

 

Appreciate any assistance!

4 REPLIES 4
Reeza
Super User
You need to restructure your data for starters, put each run on it's own line and add an indicator that is for run1/run2 then pipe that to the datalabel option on the hbar/hbarparm statement. That will get you the labels.

For the points, similar idea, use a scatter statement with the value of the Distance to get the same chart, with one variable for Water/Rest as well.
snapbolt
Calcite | Level 5

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;

 

snapbolt_0-1632450408855.png

 

 

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:

 

snapbolt_3-1632451077982.png

 

The remaining issue is with the legend. I need the marked section to look like this:

 

snapbolt_4-1632451216828.png

 

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.

 

snapbolt_2-1632450781627.png

 

 

snapbolt_1-1632450754322.png

 

 

 

 

 

Reeza
Super User
Look at the KEYLEGEND statement to have control over the legend via SGPLOT.
snapbolt
Calcite | Level 5

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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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