See Crunch Automation basics for more information.
The CREATE CONVERT command uses the same syntax and rules as the CONVERT command, but creates a new variable (hence necessitating the AS argument in the command). In other words, CREATE CONVERT produces a derivation. CONVERT, by contrast, is a schema command that changes an existing variable’s type. For all the syntax and rule options, please refer to the CONVERT command page.
You can use CREATE CONVERT when you don’t want to alter a dataset’s schema but want to instead create a new variable with a converted type. For example, you may have data that has been imported as numeric values and you need to create a new variable that is represented categorically (see the example in the use case section below). In the context of appending data, the append will align based on the source variable (numeric) and automatically update the derived variable.
CREATE CONVERT alias, ..., alias TO CATEGORICAL WITH
VALUE "string"|number TO "label" [CODE code [NUMERIC VALUE <float>] [MISSING]],
...,
VALUE "string"|number TO "label" [CODE code [NUMERIC VALUE <float>] [MISSING]]
[ELSE INTO NULL]
AS alias, ..., alias
[TITLE "string", ..., "string"]
[DESCRIPTION "string", ..., "string" | COPY]
[NOTES "string", ..., "string" | COPY];
CREATE CONVERT
alias, ..., alias TO DATETIME FORMAT "string"
AS alias, ..., alias
[TITLE "string", ..., "string"]
[DESCRIPTION "string", ..., "string" | COPY]
[NOTES "string", ..., "string" | COPY];
CREATE CONVERT
alias, ..., alias TO NUMERIC
AS alias, ..., alias
[TITLE "string", ..., "string"]
[DESCRIPTION "string", ..., "string" | COPY]
[NOTES "string", ..., "string" | COPY];
CREATE CONVERT
alias, ..., alias TO TEXT
AS alias, ..., alias
[TITLE "string", ..., "string"]
[DESCRIPTION "string", ..., "string" | COPY]
[NOTES "string", ..., "string" | COPY];
Using the DATE attribute
The CREATE CONVERT command also allows you to create categorical-date variables by specifying a DATE attribute to each of the categories defined in the command.
Creating a categorical date variable requires that all the non-missing categories contain a DATE attribute that should contain a valid and unique ISO-8601 date string:
CREATE CONVERT my_num TO CATEGORICAL WITH
value 1 to "Level 1" DATE "2022-01-21",
value 2 to "Level 2" DATE "2022-01-22",
value 3 to "Level 3" DATE "2022-01-23",
value 4 to "Level 4" DATE "2022-01-24",
value 5 to "Level 5" CODE 500 DATE "2022-01-25" MISSING
AS my_cat;
Use cases
There are many applications of CREATE CONVERT in terms of creating a new variable of a different type. Here we cover a couple of common scenarios.
Creating a categorical representation of numeric data
In this example, the SPSS datafile had set the variable type to be numeric for a set of three variables. These are actually a 5-point agreement scale, but the metadata to show they are categorical (and belong to the one variable set) has been lost in the SPSS file. Using Crunch Automation, we can create a derived version of the numeric variables that are categorical:
CREATE CONVERT q4a, q4b, q4c TO CATEGORICAL
WITH
VALUE 1 TO "Strongly disagree" CODE 1 NUMERIC VALUE 10,
VALUE 2 TO "Disagree" CODE 2,
VALUE 3 TO "Neither agree nor disagree" CODE 3,
VALUE 4 TO "Agree" CODE 4,
VALUE 5 TO "Strongly agree" CODE 5
AS q4a_cat, q4b_cat, q4c_cat;
Using a derivation (CREATE CONVERT rather than REPLACE CONVERT) means the original source variables (numeric) will remain unchanged, which could be useful in the context of appending (as the incoming SPSS data will likely have these set to numeric as well):
From here, it’s clear these are meant to form part of a categorical array, so the next command will do that and hide the inputs, borrowing the descriptions for the subvariable names:
CREATE CATEGORICAL ARRAY
q4a_cat, q4b_cat, q4c_cat
LABELS USE DESCRIPTION
HIDE INPUTS
AS q4_array
TITLE “World issues: agreement”;
Creating a numeric representation of categorical data
In the 2019 Mobile Technology and Home Broadband 2019 example study, there is a variable that represents age (alias = age). This is a categorical variable, which each category representing the age of respondents. Each category has a numeric value, which aligns with the age indicated on the category’s label (e.g., “18 years”). You may wish to transform this to a numeric variable (for example, if you then wish to use the CREATE CATEGORICAL CUT command on the resultant numeric variable).
The Properties of the categorical variable age are shown in the following:
Running the following command creates a numeric variable:
CREATE CONVERT age TO NUMERIC
AS age_num;