Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Graphics
- /
- SGPLOT legend doesn't show empty groups

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 05-15-2015 02:58 PM
(1707 views)

Hello all,

I am currently writing a quick macro to compute bar charts. Each bar chart corresponds to a specific type within a categorical variable, with a bar for each year and the response being the rate of this type occurrence in said year, relative to the population. I have almost everything plotting how I want it, but for consistency, I want the legend to include the year even if the rate was zero for that year. For example, there are 11 years total, but one of the types only occurs in 2013, so the plot shows just one, really wide, bar and the legend only shows year 2013. I want the bar to be scaled to match the size of a bar in a type that occurs every year and I want the legend to list all 11 years.

My code so far is;

%macro plots (val=);

proc sgplot data=ABC (where=(X="&val"));

vbar X/group=YEAR response=RATE

groupdisplay=cluster grouporder=ascending;

yaxis values=(0 to 2.5 by 0.25);

run;

%mend plots;

Here are some images.

Thanks everyone!

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Given your scenario, I would create a data step where I would iterate by each value of X and have a "year" loop to init the "response" column to 0. Then, use PROC APPEND to append the real data set to the end of that data set. The result should create entries for all combinations of category and group such that the bars and legend behave as you requested without affecting the original data calculations in VBAR. GIve that a try and let me know if it works out for you.

Thanks!

Dan

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Given your scenario, I would create a data step where I would iterate by each value of X and have a "year" loop to init the "response" column to 0. Then, use PROC APPEND to append the real data set to the end of that data set. The result should create entries for all combinations of category and group such that the bars and legend behave as you requested without affecting the original data calculations in VBAR. GIve that a try and let me know if it works out for you.

Thanks!

Dan

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you Dan!!

Although I did not use your method, you pointed out my mistake. I used proc freq to output the data set and assumed 0 counts would be included in this set. I went back and used the SPARSE option in proc freq to include these 0's, effectively doing what you suggested.

Thanks so much for the quick response!

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.