Schema commands
CONVERT
The CONVERT command allows you to confirm or alter the types of the mentioned variables, to ensure they have the expected type later in the script.
Only in cases of converting to categorical is it possible to indicate which codes should be assigned to each of the values of the original column.
In situations where the input variables are the same as the target type, it won’t fail and instead will work as a confirmation of the type.
If the input variable is a derived categorical variable, it will return an error since it’s not possible to perform casting with options to a derived variable.
CONVERT alias, ..., alias TO (CATEGORICAL|NUMERIC|TEXT) [WITH "label" TO code [MISSING], ..., "label" TO code [MISSING]];
Example
CONVERT var1, var2 TO NUMERIC; CONVERT var3, var4 TO CATEGORICAL WITH "a" TO 1, "b" TO 2;
RENAME
When handling unprocessed datasets, the aliases provided from the original file may not be adequate for the team that will process them. The RENAME command allows you to change the alias of variables.
RENAME alias, ..., alias TO alias, ..., alias;
Example
RENAME var1abc, var2cde TO var1, var2;
Non schema commands
REPLACE
This command allows you to replace the name, notes, and description for a list of variable aliases.
REPLACE
alias, ...., alias
WITH
(NOTES|NAME|DESCRIPTION) "string"
...
(NOTES|NAME|DESCRIPTION) "string";
REPLACE (NOTES|NAME|DESCRIPTION) IN
alias, ...., alias
(LIKE|REGEX|REGEXP) "string" WITH "string";
Example
REPLACE random, pk WITH NOTES "Do not use these"; REPLACE Age WITH NAME "Respondent Age"; REPLACE alias1 WITH NAME "my var 1" NOTES "My notable notes"; REPLACE q1...q5 WITH DESCRIPTION "subvariables"; REPLACE DESCRIPTION IN var1, var2 LIKE "Remove me" WITH "Replaced"; REPLACE DESCRIPTION IN var1, var2 LIKE "%begin" WITH "begin"; REPLACE DESCRIPTION IN var1, var2 REGEX "[abc]" WITH "+"; REPLACE DESCRIPTION IN var1, var2 REGEXP "last.*" WITH "last";
SET EXCLUSION
When necessary, this command allows you to set the dataset’s exclusion filter to the desired expression.
SET EXCLUSION condition;
Example
SET EXCLUSION my_num1 > 10;
CREATE FILTER
Allows you to create arbitrary public filters for the dataset.
CREATE FILTER condition NAME "string";
Example
CREATE FILTER wave_date >= 2019-07-01 AND wave_date < 2019-10-01 NAME "2019Q3";
CREATE MULTITABLE
Allows you to create arbitrary public multitables for the dataset, which indicates the labels and categories to hide for each variable.
CREATE MULTITABLE alias [([LABELS code="string", ..., code="string"] [HIDE code, ..., code])] ... alias [([LABELS code="string", ..., code="string"] [HIDE code, ..., code])] NAME "string";
Example
CREATE MULTITABLE
age4 (HIDE "Missing"),
gender (LABELS 1="M", 2="F"),
multrace,
wave
NAME "Demographics";
ORGANIZE
Newly uploaded datasets will have all its variables flat at the root folder without any organization. The ORGANIZE command allows you to put groups of variables into folders, leveraging nested trees. This command also allows you to organize variables under the secure variables tree.
ORGANIZE alias, ..., alias INTO [SECURE|HIDDEN] "string";
Example
ORGANIZE gender, age, country INTO "Basic|Demographics";
DISPLAY
The DISPLAY command sets subtotal properties to be displayed on card view for categorical variables. It works by indicating what subtotals to compute, based on a list of categories and where to show them on the variable card.
DISPLAY
alias, ..., alias
SUBTOTAL CODES code, ..., code [MINUS code, ..., code] LABEL "string" [AFTER CODE code]
...
SUBTOTAL CODES code, ..., code [MINUS code, ..., code] LABEL "string" [AFTER CODE code]
[POSITION TOP|BOTTOM];
Example
DISPLAY
approval, app2, app3, app
SUBTOTAL CODES 1,2 LABEL "Approve"
SUBTOTAL CODES 3,4 LABEL "Disapprove" AFTER CODE 2
SUBTOTAL CODES 1,2 MINUS CODES 3,4 LABEL "Net approval"
POSITION BOTTOM;
LABEL CATEGORIES
Allows you to change the name of the categories on categorical variables. When used on multiple variables, all should have the exact same categories (in number and codes). The changes will happen in place on the same variable.
LABEL CATEGORIES
alias, ..., alias
WITH code AS "string", ..., code AS "string";
Example
LABEL CATEGORIES
fav_browser, laptop_brand, fav_os
WITH 9 AS "Not answered", 8 AS "Unknown";
REORDER CATEGORIES
Use the REORDER command to change the order of categorical variables. It will mutate the variable in place instead of creating a new variable. When used on multiple variables, they should all have the same categories (in number and codes). The category codes not mentioned on the order will be kept in the same order at the of the category list.
REORDER CATEGORIES
alias, ..., alias
ORDERED code, ..., code;
Example
REORDER CATEGORIES
fav_browser, laptop_brand, fav_os
ORDERED 1, 2, 8, 9;
CREATE CATEGORICAL RECODE
Often, surveys will provide more categories than are desired for an analysis, so it is helpful to combine categories. For example, “agreement” type scales will give “very”, “somewhat”, and “slightly” options that you want to combine into an overall “agree” category.
CREATE CATEGORICAL
RECODE alias, ..., alias
MAPPING
code, ..., code INTO "label" [CODE value [MISSING]]
code, ..., code INTO "label" [CODE value [MISSING]]
[ELSE "label" [INTO CODE value [MISSING]]|ELSE INTO NULL]
AS alias, ..., alias
[NAME "string", ..., "string"]
[DESCRIPTION "string", ..., "string"]
[NOTES "string", ..., "string"];
Example
CREATE CATEGORICAL
RECODE v5, v6
MAPPING
4, 5 INTO "Likely" CODE 1
3 INTO "Neutral" CODE 2
1, 2 INTO "Unlikely" CODE 3
ELSE INTO "Unaware" CODE 99 MISSING
AS "purchaselikelihood_1", "purchaselikelihood_2"
NAME "Purchase likelihood A combined", "Purchase likelihood B combined"
NOTES "Asked of those aware of each product";
CREATE CATEGORICAL CASE
This allows you to assign specific values to categories based on a sequence of logical conditions. This is useful for creating segments or defining conditions based on multiple other variables, and not just recoding one. Rows that do not match any WHEN condition will be missing.
CREATE CATEGORICAL
CASE
WHEN condition THEN "label" [CODE value [MISSING]]
...
WHEN condition THEN "label" [CODE value [MISSING]]
[ELSE "string" [CODE value [MISSING]|ELSE INTO NULL]
END
AS alias
[NAME "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Example
CREATE CATEGORICAL
CASE
WHEN pid3 = "Democrat" AND ptystrength = "Strong"
THEN "Strong Democrat" CODE 1
WHEN pid3 = "Democrat" AND ptystrength = "Weak"
THEN "Weak Democrat" CODE 2
WHEN pid3 = "Independent" AND ptycloser = "Democrat"
THEN "Lean Democrat" CODE 3
WHEN pid3 = "Independent" AND ptycloser = "Neither"
THEN "Independent" CODE 4
WHEN pid3 = "Independent" AND ptycloser = "Republican"
THEN "Lean Republican" CODE 5
WHEN pid3 = "Republican" AND ptystrength = "Weak"
THEN "Weak Republican" CODE 6
WHEN pid3 = "Republican" AND ptystrength = "Strong"
THEN "Strong Republican" CODE 7
ELSE "Unknown party" CODE 9 MISSING
END
AS "pid7"
NAME "Party Identification"
DESCRIPTION "7 category party identification scale"
NOTES "Recoded from pid3, ptystrength, and ptycloser";
CREATE CATEGORICAL CASE … THEN VARIABLE
CREATE CATEGORICAL
CASE
WHEN condition THEN VARIABLE alias
...
WHEN condition THEN VARIABLE alias
[ELSE "string" [CODE value [MISSING]|ELSE INTO NULL]
END
AS alias
[NAME "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Example
CREATE CATEGORICAL
CASE
WHEN a = 9 THEN "Not asked" CODE 9 MISSING
WHEN a = 1 THEN VARIABLE a_1 #1=high 2=med 3=low
WHEN a = 2 THEN VARIABLE a_2 #1=low 2=med 3=high
END
AS my_case
NAME "My filled case";
CREATE CATEGORICAL ARRAY
Often, surveys will ask similar questions with identical response options about a number of things — ratings of items is a common example of a “grid” of related columns of data. These can be grouped and displayed together as a Categorical Array in Crunch.
CREATE CATEGORICAL ARRAY
alias, ..., alias
LABELS "string", ..., "string"
AS alias
[NAME "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Example
CREATE CATEGORICAL ARRAY
rating_1, rating_2, rating_3
LABELS "Apple", "Lenovo", "Dell"
AS computer_ratings
DESCRIPTION "How likely are you to recommend your __ computer to friends?"
NAME "Ratings of Computers";
CREATE MULTIPLE DICHOTOMY FROM
This command creates new derived Multiple Response variables using an existing categorical array variable as input by indicating which code to use as a selected category.
The LABELS argument allows you to indicate alternative names that the subvariables will have on the new Multiple Response variable.
When using multiple inputs, you must indicate the same number of output aliases for each of the newly created multiple response variables.
CREATE MULTIPLE DICHOTOMY FROM
alias, ..., alias
[LABELS "string", ..., "string"]
SELECTED code, ..., code
AS alias, ..., alias
[NAME "string", ..., "string"]
[DESCRIPTION "string", ..., "string"]
[NOTES "string", ..., "string"];
Example
CREATE MULTIPLE DICHOTOMY FROM
array1, array2
LABELS "subvar1", "subvar2", "subvar3"
SELECTED 1, 3
AS my_mr1, my_mr2
NAME "Multiple Response 1", "Multiple Response 2";
CREATE MULTIPLE DICHOTOMY
Allows you to create a Multiple response variable from a list of categorical subvariables. All the mentioned subvariables MUST have the same categories.
CREATE MULTIPLE DICHOTOMY
alias, ..., alias
[LABELS "string", ..., "string"]
SELECTED code, ..., code
AS alias
[NAME "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Example
CREATE MULTIPLE DICHOTOMY
cat1, cat2, cat3
LABELS "Resp1", "Resp2", "Resp3"
SELECTED 1
AS mymr
NAME "My Responses";
CREATE NUMERIC
This command allows you to create a numeric variable from an arithmetic numeric expression.
CREATE NUMERIC
expression
AS alias
[NAME "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Example
CREATE NUMERIC
(2020 - DoB) * 2
AS double_age
NAME "Age doubled";
CREATE CATEGORICAL CUT
This converts a numeric variable to a categorical one by defining boundaries between ranges. It’s useful to make numeric values simple to work with in tables, and are meaningful for their domain: respondent “age” in a small number of bins that become the basis of comparisons. In practice, surveys often use a hybrid numeric variable with effectively categorical sentinel values to indicate special missing values such as “-1” for an otherwise positive range; or some “high / out of bounds” number such as “999”. Missing values indicated here are compared using strict equality and result in assignment to missing categories in the result, with optionally user-specified codes.
CREATE CATEGORICAL CUT
alias, ..., alias
BREAKS MIN, number, ..., number MAX
[LABELS "string", ..., "string"]
[CODES code, ..., code]
[MISSING id ["label"] [CODE code], ..., id ["label"] [CODE code]]
AS alias, ..., alias
[NAME "string", ..., "string"]
[DESCRIPTION "string", ..., "string"]
[NOTES "string", ..., "string"];
Example
CREATE CATEGORICAL CUT
my_num
BREAKS MIN, 10, 100, 1000, MAX
CODES 1, 2, 3, 4
MISSING 66 CODE 3, 67 "nope" CODE 2, -9 CODE 666
AS my_cut;
CREATE WEIGHT
Allows to create a variable to be used as a weight via the rake function. Permits to to use multiple categorical variables as input and set different weights for each of categories.
CREATE WEIGHT
RAKE
alias TARGETS value=float, ..., value=float
alias TARGETS value=float, ..., value=float
[TRIM MIN=float MAX=float]
AS alias
[NAME "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Example
CREATE WEIGHT
RAKE
var0 TARGETS 1=0.4, 2=0.6
var1 TARGETS 1=0.3, 2=0.4, 3=0.3
AS my_wt_var
NAME "Name"
DESCRIPTION "Description"
NOTES "Notes";
SET MISSING
Use this command to change or ensure that certain categories in variables are missing or not.
SET MISSING alias, ..., alias WITH code, ..., code AS [NOT] MISSING;
Example
SET MISSING Age, Gender WITH 98, 99 AS MISSING;