BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jc3992
Pyrite | Level 9

Hello everyone,

 

The code is as below:

*Create two way table with Age grouped into two categories;
PROC FREQ DATA = books;
   TITLE 'Patron Age by Book Type: Two Age Groups';
   TABLES BookType * Age / NOPERCENT NOROW NOCOL;
   FORMAT Age agegpb. BookType $typ.;
RUN;


/* How does PROC FREQ order the categories? For example,
	why does Non-Fiction get listed before Fiction?

The data-set is as below:

 

17 sci 9 bio 28 fic 50 mys 13 fic 32 fic 67 fic 81 non 38 non
53 non 16 sci 15 bio 61 fic 52 ref 22 mys 76 bio 37 fic 86 fic
49 mys 78 non 45 sci 64 bio 8 fic 11 non 41 fic 46 ref 69 fic
34 fic 26 mys 23 sci 74 ref 15 sci 27 fic 23 mys 63 fic 78 non
40 bio 12 fic 29 fic 54 mys 67 fic 60 fic 38 sci 42 fic 80 fic

 

I wonder how to answer the question:

"Why does Non-Fiction get listed before Fiction"?

 

Thank you~

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The answer depends on two factors, taken together:

 

  • The definition of the $TYP format, plus
  • The actual values of BookType

Within the groupings defined by $TYP, SAS is storing all the values in each group using a single value.  For example, part of the format definition might be:

 

'sci', 'bio', 'non', 'ref' = 'Non-Fiction'

'fic', 'mys' = 'Fiction'

 

SAS stores the Non-Fiction category using the smallest of the four values (sci bio non ref) that is actually found in the data.

 

You do have control over that, since PROC FREQ supports the ORDER= option.  The default is INTERNAL (order by the internal value of the values).  But you can switch to ORDER=FORMATTED to force Fiction to print before Non-Fiction.

 

Similarly, SAS stores the Fiction category using the smallest of the two values (fic mys) that is actually found in the data.

 

The order of the rows is determined by the smallest value in the data for each category, and prints in that order.  So depending on the values found in the data, the row order could change.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

The answer depends on two factors, taken together:

 

  • The definition of the $TYP format, plus
  • The actual values of BookType

Within the groupings defined by $TYP, SAS is storing all the values in each group using a single value.  For example, part of the format definition might be:

 

'sci', 'bio', 'non', 'ref' = 'Non-Fiction'

'fic', 'mys' = 'Fiction'

 

SAS stores the Non-Fiction category using the smallest of the four values (sci bio non ref) that is actually found in the data.

 

You do have control over that, since PROC FREQ supports the ORDER= option.  The default is INTERNAL (order by the internal value of the values).  But you can switch to ORDER=FORMATTED to force Fiction to print before Non-Fiction.

 

Similarly, SAS stores the Fiction category using the smallest of the two values (fic mys) that is actually found in the data.

 

The order of the rows is determined by the smallest value in the data for each category, and prints in that order.  So depending on the values found in the data, the row order could change.

jc3992
Pyrite | Level 9

Thank you!!

Very clear!

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!

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
  • 2 replies
  • 367 views
  • 1 like
  • 2 in conversation