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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 1729 views
  • 0 likes
  • 3 in conversation