BookmarkSubscribeRSS Feed
apple
Calcite | Level 5
Although I have used “(notsorted)” & ORDER=DATA PRELOADFMT to preserve the order of my user-defined format, why does Females still come before Males in my output table?
 
PROC FORMAT;
 
VALUE $SEXB (notsorted)
 
OTHER ='Males'
'F'='Females'
;
 
Run;
 
PROC TABULATE DATA=test F=COMMA9. FORMCHAR='           ' MISSING ;
CLASS ID AGE  SEX ETH/ ORDER=DATA PRELOADFMT MLF MISSING;
 
KEYLABEL                N=' '
                                SUM=' '
                                ALL ='Total';
TABLE (ALL ID=' '),(ALL AGE=' '),(ALL ETH=' ')*(ALL SEX=' ')/ PRINTMISS MISSTEXT='0';
FORMAT ID $ID. AGE AGED. ETH $ETH. SEX $SEXB.;
 
RUN;
 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

BECAUSE F SORTS BEFORE M.

 

Please avoid coding all in uppercase you can see how it does not read as easy and sounds like shouting.  The character F has a lower ASCII code than M, therefore it appears before M.  For these types of scenarios, it is a good idea to have the underlying data as a numeric, and format that value to be the text you want, e.g. (and again note the code casing/indentation):

data have;
  set have;
  sexc=ifn(sex="F",2,1);
run;

proc format;
  value sex
    1="Females"
    2="Males";
run;

proc tabulate data=have ...;
  class id age sexc ...;
  ...
  format sexc sex.;
run;

You will see I create a coded version of the data, with a 1 or 2, then apply a format of the text to this numeric code.  

Astounding
PROC Star

As long as you are willing to specify ORDER=DATA, try sorting your data set before running PROC TABULATE.  The sorted order could be BY DESCENDING SEX.

 

As you may have noticed, ORDER=DATA affects your other CLASS variables as well.

Ksharp
Super User

1) Try put order=data at PROC TABULATE

PROC TABULATE DATA=test ORDER=DATA ;
 
 
2) Put some white blanks before Males.
PROC FORMAT;
VALUE $SEXB 
OTHER ='  Males'
'F'='Females'
;
Run;
PROC TABULATE DATA=test ORDER=formatted ;
BrunoMueller
SAS Super FREQ

Have a look at the CLASSDATA= option on Proc TABULATE. It allows you to specify a datasets that will determine the ordering of the class variable values, see also sample below.

 

data classOrder;
  infile cards dlm="," truncover;
  input
   type : $8. origin : $6.
  ;
cards;
Hybrid,Europe
Hybrid,USA
Hybrid,Asia
Wagon,Europe
Truck,Europe
SUV,Europe
Sedan,Europe
Sports,Europe
;

proc tabulate
  data=sashelp.cars 
  classdata=classorder
  order=data
;
  class type origin;

  table type, origin ;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 2140 views
  • 0 likes
  • 5 in conversation