SPSS Statistics

 View Only

 Generating syntax from existing categorical variables

Jordan Troup's profile image
Jordan Troup posted 08/05/26 10:15 AM

Hi all

Please let me know if this isn't a suitable place to post this question. 

In SPSS, I have a good number of categorical variables containing values and value labels. 

In excel, I have the same variables but the data is in string format.

E.g., in the SPSS file, if there were a categorical variable called 'pet_type', and the value '2' had the value label 'dog', then in the excel file, in the 'pet' column, there would just be the word 'dog'.

I'm looking for the easiest and least error-prone method of getting the excel data into the SPSS file.

Ideally, I need to turn all the 'dog's into the value '2' and then merge this into the SPSS database, and I could write some syntax for this. However, I have dozens of different variables each with multiple unique categories of data and so writing this code would be pretty time consuming.

Best case I can think of is that there would be an easy way to generate syntax automatically from the existing SPSS variables e.g., something that takes the values and value labels of the categorical variables and automatically generates a syntax that would transform any string data into this format. Does this exist?

Or does anyone have a better method to suggest?

Thanks very much

Jon Peck's profile image
Jon Peck IBM Champion

Take a look at AUTORECODE  - Transform > Automatic Recode.

If you have a lot of variables to do, you can use syntax to make it easier to provide the new names, e.g.,

AUTORECODE VARIABLES=V1 V2 V3 /INTO=NEWV1 TO NEWV3 /PRINT.

Kirill Orlov's profile image
Kirill Orlov

If you want to get it fast and you agree to redefine the code-label relationship, you may 1) save your SPSS file in Excel format while selecting "Save values as labels where defined". 2) Merge this Excel file with your Excel file: they are expected to have the same text values, which are the labels, are they in fact? 3) Open in SPSS. 4) Apply Autorecode; the code-label schema will be alphabetical order.