SPSS Statistics

 View Only

 Syntax for specifying category order in frequency table

Andrew Munn's profile image
Andrew Munn posted Thu February 06, 2025 05:19 PM

Hi, everyone. I'm trying to put together a series of frequency tables in SPSS from survey data, and I can't figure out how to specify the order I want the rows in. The answer options for a lot of the questions in the survey are:

Extremely confident

Confident

Neither confident nor unconfident

Unconfident

Extremely unconfident

The following code sorts the data the way I want it, and then creates a frequency table, but the category order doesn't get carried through to the frequency table:

missing values variable_name ('')

recode variable_name

   ("Extremely confident" = 1)

   ("Confident" = 2)

   ("Neither confident nor unconfident" = 3)

   ("Unconfident" = 4)

   ("Extremely unconfident" = 5)

   (convert) into response_order_numeric.

execute.

sort cases by response_order_numeric(D)

frequencies variable_name

The categories in the frequency tables are coming out in alphabetical order:

Confident

Extremely confident

Extremely unconfident

Neither confident nor unconfident

Unconfident

Obviously, not ideal.

Does anyone know how to specify a custom category order for a frequency table using syntax?

Thanks in advance.

Robert Lundqvist's profile image
Robert Lundqvist

It seems as if your making a table for the old variable, not the new recoded one. The line "frequencies variable_name", shouldn't it be "frequencies response_order_numeric"?

Jon Peck's profile image
Jon Peck IBM Champion

Frequencies does not give you custom control over the category order other than by count or alpha order, but Custom Tables lets you specify the category order.  See the  Categories and Totals subdialog

Here is sample syntax

CTABLES
  /TABLE jobcat
  /CATEGORIES VARIABLES=jobcat [2, 3, 1, OTHERNM] EMPTY=INCLUDE.

Art Jack's profile image
Art Jack

you could also recode the string to a numeric value & place the corresponding value labels on top.  they'll show up in numeric order.

Andrew Munn's profile image
Andrew Munn

Thanks for your help, everyone. It turns out the SPSS license I have through my job doesn't let me use custom tables (that costs extra, I guess), but recoding the string variable to a new numeric variable and adding in the original variable's categories as value labels worked. For anyone who has this problem in the future, this is how I ended up doing it:

missing values variable_name ('').

recode variable_name

    ("Extremely confident" = 1)

    ("Confident" = 2)

    ("Neither confident nor unconfident" = 3)

    ("Unconfident" = 4)

    ("Extremely unconfident" = 5)

    (convert) into variable_name_numeric.

value labels variable_name_numeric    

    1 "Extremely confident"

    2 "Confident"

    3 "Neither confident nor unconfident"

    4 "Unconfident"

    5 "Extremely unconfident".

sort cases by variable_name_numeric(A).

frequencies variable_name_numeric.

execute.

Jon Peck's profile image
Jon Peck IBM Champion

CTABLES is part of the Standard Edition of SPSS.  There is a Base version that is cheaper, but it might be worth considering moving up to the Standard Edition if you use Statostics a lot.