BookmarkSubscribeRSS Feed
xxformat_com
Barite | Level 11

This kind of code allows to list Male before Female in a proc means

 

 

proc sort data=sashelp.class out=class;
   by sex;
run;

proc format;
    value $sex (notsorted) 'M'='Male'
                           'F'='Female';
run;

proc means data=class;
    class sex / preloadfmt order=data;
    var height;
    format sex $sex.;
run;

 

 

But as there is currently no preloadfmt options with the xaxis statement, the following code has no effect on the result

 

ods graphics / width=15cm height=10cm;

proc sgplot data=class;
    vbar sex / missing;
    
    xaxis discreteorder=data;*formatted;
    format sex $sex.;
run;

ods graphics off;

I know other ways to modify the order of the discrete values in the axis (vbarparm, xaxis values(), etc) but I would just like to know if it would work with vbar in a future SAS release.

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

Please try the below code

 

proc format;
    value sexn (notsorted) 2='Male'
                           1='Female';
run;

data class;
set sashelp.class;
if sex='M' then sexn=2;
else if sex='F' then sexn=1;
run;


proc sgplot data=class;
    vbar sexn / missing  ;
    
    xaxis discreteorder=formatted;
    format sexn sexn.;
run;
Thanks,
Jag
xxformat_com
Barite | Level 11
Have you tried it? Female are still before Male in your example.
Jagadishkatam
Amethyst | Level 16

oh ok, i thought you want female before male, if you want male before female then the code you posted is fine. I updated my code to generate the male before female

 

proc format;
    value sexn (notsorted) 1='Male'
                           2='Female';
run;

data class;
set sashelp.class;
if sex='M' then sexn=1;
else if sex='F' then sexn=2;
run;


proc sgplot data=class;
    vbar sexn / missing  ;
    
    xaxis discreteorder=formatted;
    format sexn sexn.;
run;
Thanks,
Jag
xxformat_com
Barite | Level 11
Please try your code first and you will see that Female are still before Male.
Jagadishkatam
Amethyst | Level 16

Oh yeah, check this

 

 

proc sgplot data=class;
    vbar sexn / missing  ;
    
    xaxis discreteorder=data;
    format sexn sexn.;
run;
Thanks,
Jag
xxformat_com
Barite | Level 11
Yes it works when we recode data. There are even faster ways using values=('M' 'F' ' ') in the xaxis statement. But my question was not to find an alternative solution. My question was : "I know other ways to modify the order of the discrete values in the axis but I would just like to ensure that my assumption is correct and that potentially it would work in a future SAS release."
Ksharp
Super User

How about this one ?

 

 

proc sort data=sashelp.class out=class;
   by sex;
run;

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


proc sgplot data=class;
    vbar sex / missing;
        format sex $sex.;
xaxis discreteorder=formatted ;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2231 views
  • 0 likes
  • 3 in conversation