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;
@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:
Hope this helps!
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.
@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:
Hope this helps!
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.
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?
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.
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?
Thanks ballard, I didn't see that.
I wish it would better documented in the xaxis statment page by the discreteorder= option .
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;
In this case, you can also maximize people's ability to read your plot by turning off the legend, which is redundant now.
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.