Question about "Proc Freq"

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 112
Accepted Solution

Question about "Proc Freq"

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~


Accepted Solutions
Solution
‎03-05-2018 10:03 PM
Super User
Posts: 6,413

Re: Question about "Proc Freq"

[ Edited ]

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


All Replies
Solution
‎03-05-2018 10:03 PM
Super User
Posts: 6,413

Re: Question about "Proc Freq"

[ Edited ]

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.

Frequent Contributor
Posts: 112

Re: Question about "Proc Freq"

Posted in reply to Astounding

Thank you!!

Very clear!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 110 views
  • 1 like
  • 2 in conversation