BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xxformat_com
Barite | Level 11

I'm wondering why we get the same result with both discreteorder=unformatted and discreteorder=formatted.

 

With unformatted I would have expect something like

2-Female (F), 1-Male (M) and 3-Missing (X)

not

1-Male (M), 2-Female (F), 3-Missing (X)

 

proc format;
    value $sex 'F' = '2-Female'
               'M' = '1-Male'
               'X' = '3-Missing';
run;

data class;
    set sashelp.class;
    if age=13 then sex='X';
run;

*format statement only;
title 'discreteorder=unformatted';
proc sgplot data=class;
    vbarbasic sex / group=sex;
    format sex $sex.;
    xaxis discreteorder=unformatted;
run;


title 'discreteorder=formatted';
proc sgplot data=class;
    vbarbasic sex / group=sex;
    format sex $sex.;
    xaxis discreteorder=formatted;
run;
title;



with valuesformat= it would work as expected

title 'discreteorder=unformatted';
proc sgplot data=class;
    vbarbasic sex / group=sex;
    xaxis discreteorder=unformatted valuesformat=$sex.;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

@ballardw is correct -- the DISCRETEORDER option will not work with VBARBASIC/HBARBASIC.

 

As a general principle, the VBAR statement should be favored over the VBARBASIC statement, except for two notable situations:

  1. You want to overlay a plot on top of the bar chart that is not allowed by the VBAR statement. If you compute the statistics outside of SGPLOT, you would normally use a VBARPARM statement for this situation. However, if the statistics are simple (freq, sum , and mean), VBARBASIC is a good statement to use instead of having to do a separate compute step.
  2. You want to overlay a non-grouped line chart on a grouped bar chart. For this situation, you would normally compute the statistics outside of SGPLOT and use a SERIES and a VBARPARM. However, using VBABASIC might save you one compute step.

Hope this helps!

View solution in original post

10 REPLIES 10
ballardw
Super User

Did you by any chance compare the results of VBAR and VBARBASIC?

 

If you check the documentation you will find this:

The bars are sorted in data order based on the category values. The sort order cannot be changed. This arrangement of the bars differs from the VBAR statement, which by default sorts bars in alphabetical order based on the category values.

Which looks to me like Unformatted and Formatted may be treated similarly without additional instructions.

 

DanH_sas
SAS Super FREQ

@ballardw is correct -- the DISCRETEORDER option will not work with VBARBASIC/HBARBASIC.

 

As a general principle, the VBAR statement should be favored over the VBARBASIC statement, except for two notable situations:

  1. You want to overlay a plot on top of the bar chart that is not allowed by the VBAR statement. If you compute the statistics outside of SGPLOT, you would normally use a VBARPARM statement for this situation. However, if the statistics are simple (freq, sum , and mean), VBARBASIC is a good statement to use instead of having to do a separate compute step.
  2. You want to overlay a non-grouped line chart on a grouped bar chart. For this situation, you would normally compute the statistics outside of SGPLOT and use a SERIES and a VBARPARM. However, using VBABASIC might save you one compute step.

Hope this helps!

xxformat_com
Barite | Level 11

Would you consider writing a great blog post on when to use vbar, vbarparm and vbarbasic?

I find it so badly documented.

 

Somehow I came accross issues with vbar and statements like vbar is mainly following the old proc gchart approach and decided to go for vbarbasic everywhere (which costs me hours of work to update things) in my code and documentation due to that :(. I really don't feel like going back to vbar now and it would be great other people won't face the same issue. Before today, the only issue I had with vbarbasic was the fact that limits options are not available there for some reason.

ps. As far as I can remember options like tip= are more intuitive with vbarbasic than vbar. vbar are a lot of options which are only there to compute the statistics in the same procedure as the plot. During the development having to recompute each time the statistics to generate the plot does not really appeal to me. So I don't really value this additional feature of vbar. What other feature of vbar would be valuable and not available in vbarparm/vbarbasic? I'm not sure.

DanH_sas
SAS Super FREQ

I'll see if I can put something together.

You mentioned that you had issues with the VBAR/HBAR statements. What kind of issues were you having?

xxformat_com
Barite | Level 11
I cannot remember. That's already a few years ago. If I do find it again, I'll let you know.
xxformat_com
Barite | Level 11

ps. it works on some occasions with vbarbasic even if the documentation does not support it e.g. there is a difference when discreteorder=data and discreteorder=unformatted when no format is used.

xxformat_com
Barite | Level 11

Given that discreteorder= is partially working and that it is also possible to define the order of the bars using values=, could it be that the statement in the online document is an old statement which was just not updated?

xxformat_com
Barite | Level 11

Thanks ballard, I didn't see that.

I wish it would better documented in the xaxis statment page by the discreteorder= option .

PaigeMiller
Diamond | Level 26

ADVICE (which I know you didn't ask for)

 

Using '1-Male' to represent 'Male' and '2-Female' to represent 'Female' and '3-Missing' to represent 'Missing" is not a good practice in my opinion, it looks unprofessional and adds things onto the plot that do not increase the understanding of what the plot is showing. The viewers of the plot (your colleagues or your professor or your boss or perhaps even potential customers) don't need to know that inside your program you used the number 2 so you can have Female in the 2nd position. Better practice is to use 'Male' to represent 'Male' and so on, and use other tools to make the ordering of the values what you want. Example:

 

proc format;
    value $sex 'F' = 'Female'
               'M' = 'Male'
               'X' = 'Missing';
run;

data class1;
    set sashelp.class;
    if age=13 then do; order=3; sex='X'; end;
    else if sex='F' then order=2;
    else if sex='M' then order=1;
run;

proc sort data=class1;
    by order;
run;

proc sgplot data=class1;
    vbar sex / group=sex;
    format sex $sex.;
    xaxis discreteorder=data;
run;

 

 

PaigeMiller_0-1724084462295.png

 

In this case, you can also maximize people's ability to read your plot by turning off the legend, which is redundant now.

--
Paige Miller
xxformat_com
Barite | Level 11
Hi Paige, it is just a sample code to illustrate the sorting issue, not the final output where a first format can be used to define the sorting order and a second one in valuesformat to define the values displayed on the axis.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 10 replies
  • 869 views
  • 3 likes
  • 4 in conversation