Codebook Generator
List your variables one per line — name, label, type, value labels, missing codes — and get a codebook table you can paste into a README, plus syntax that applies the same labels to your data: VARIABLE LABELS, VALUE LABELS and MISSING VALUES for SPSS, factor or haven code for R. The tool also checks names and labels against SPSS limits. Free, no sign-up. Nothing you type leaves your browser.
Your variables
Fields in this order, separated by | or by tabs (paste straight from a spreadsheet): name | label | type | value labels | missing codes | notes. Only the name is required.
Type is scale, ordinal, nominal or string. Value labels are code=label pairs separated by semicolons. Missing codes are separated by commas; write a range as 997 to 999, and use LO or HI for an open end.
Base R turns value labels into factors and user-missing codes into NA. The haven style keeps the codes and labels together, as SPSS does.
Used in the R syntax. An invalid name falls back to df.
Codebook
Type or paste your variables above, or press Try an example.
Export
Run it with your data file open: VARIABLE LABELS, VALUE LABELS, MISSING VALUES and VARIABLE LEVEL. It does not create variables or set string widths.
How to Use the Codebook Generator
- 1. List your variables. One per line, with fields separated by a pipe or a tab, so you can paste straight from a spreadsheet: name, label, type, value labels, missing codes, notes. Only the name is required.
- 2. Describe the values. Write value labels as code=label pairs separated by semicolons (1=Female; 2=Male). Write missing codes separated by commas, and a range as 997 to 999. Choose scale, ordinal, nominal or string for the type.
- 3. Read the checks. The table shows what was understood. “Things to check” lists skipped pairs, repeated names and anything SPSS would reject, with the line number.
- 4. Export. Choose SPSS syntax, R syntax, a Markdown table or CSV, then copy or download. For R, pick base R or haven and name your data frame.
What a Codebook Is For
A codebook is the key to a dataset. A column named q7 and full of 1s, 2s and 9s means nothing to a reader, or to you in a year, until a document says what q7 asks, what each number stands for, and which numbers are not answers at all. A codebook records, for every variable, its name, a descriptive label, its type, the labelled values and the missing-data codes. It is the first thing a collaborator or reviewer looks for.
The table and the code come from one list, so they cannot drift apart. The tool does not read a data file: you type the definitions, and no data values are involved.
Types and Missing Codes
| Type | Use it for | In SPSS | In base R |
|---|---|---|---|
| Scale | Measured quantities: age, income, test score | VARIABLE LEVEL SCALE | as.numeric() |
| Ordinal | Ordered categories: a 1–5 agreement scale | VARIABLE LEVEL ORDINAL | ordered factor |
| Nominal | Unordered categories: gender, country | VARIABLE LEVEL NOMINAL | factor |
| String | Text: free responses, participant IDs | No level is set | as.character() |
A scale variable can still carry value labels, for example a code for “refused”. Base R keeps it numeric and does not turn it into a factor.
A missing code is a number that stands in for “no answer”: -99 for refused, -98 for don’t know, 997 to 999 for several reasons at once. Left alone, these numbers go into your means. Declaring them as user-missing tells SPSS to leave them out of calculations while keeping the codes in the file. R has no such flag in base, so the base syntax converts the codes to NA; the haven version keeps them as SPSS does.
What SPSS Allows
The checker follows IBM’s command syntax reference. A variable name can be up to 64 bytes, must begin with a letter or @, cannot begin or end with a period, and cannot be one of the reserved words ALL, AND, BY, EQ, GE, GT, LE, LT, NE, NOT, OR, TO or WITH. A name that begins with # makes a scratch variable and one that begins with $ is reserved for system variables, so the tool rejects both. Value labels can be up to 120 bytes. A numeric variable can have up to three discrete missing values, or one range with one extra discrete value, as in LO THRU 0, 999. IBM advises keeping each line of syntax within 256 characters; the tool keeps to 251 bytes, a limit people have reported SPSS warning at, so a very long label is flagged before it fails. See IBM’s variable names and VALUE LABELS pages.
Worked Example
“Try an example” loads five survey variables: a text ID, a numeric age, a nominal gender with four labelled values, a five-point ordinal item and an income variable whose codes 997 to 999 are missing. The SPSS output is four commands. VARIABLE LABELS covers all five variables, VALUE LABELS covers gender and the ordinal item, MISSING VALUES covers four (age, gender, the ordinal item and income), and VARIABLE LEVEL covers the four non-text variables. This is the MISSING VALUES command it writes:
Example: the MISSING VALUES command
MISSING VALUES age (-99) /gender (-99) /sat1 (-99, -98) /income (997 THRU 999).
In R, the base version recodes those codes to NA and turns the labelled variables into factors, with the ordinal item as an ordered factor. The haven version calls haven::labelled_spss(), whose arguments labels, na_values, na_range and label hold the same four things. The tool writes each as one line, so you can delete the variables you do not want.
Limits and Cautions
- It does not read your data. Check that every code you list really occurs, and that none occurs that you left out.
- R syntax changes your data. The base version overwrites each column. Work on a copy, and check every
as.numeric()against the raw file; a value that cannot be read as a number becomesNA, with a warning. - SPSS string variables. Strings need a width when they are created, and the syntax here does not create variables. Run it with your data open.
- Keep the codes consistent. Use the same missing codes across variables where you can, and never a code that could be a real answer.
Frequently Asked Questions
- What is a codebook in research?
- A codebook is a document that explains a dataset: what each variable is called, what it measures, how it is coded, and which codes mean missing data. Someone who has never seen the data should be able to read the codebook and interpret every column. It is sometimes called a data dictionary.
- What should a codebook include?
- At a minimum, each variable’s name, label and type, the value labels for coded variables, and the missing-data codes. Survey codebooks often add the exact question wording, the response options, who was asked, and notes on recoding or derived variables. This tool covers the first group and has a notes column for the rest.
- How do I add value labels and missing values in SPSS syntax?
- Use VALUE LABELS with a variable name followed by code and label pairs, for example VALUE LABELS gender 1 "Female" 2 "Male". Use MISSING VALUES with the variable and its codes in parentheses, for example MISSING VALUES age (-99). End each command with a period. The tool writes both, with one variable per block, separated by slashes.
- How many missing values can an SPSS variable have?
- A numeric variable can have up to three individual user-missing values, or one range of values plus one additional discrete value. A range uses THRU, and LO and HI stand for the lowest and highest values. The tool flags a variable that exceeds this.
- Should I use base R factors or haven in R?
- Use base R if you will analyse the data in R only: factors carry the labels and NA carries the missing codes. Use haven if the data came from or will return to SPSS, Stata or SAS, because labelled_spss() keeps codes, labels and user-missing values together. Haven’s own documentation describes the class as modelling SPSS’s user-defined missings.
- Is my data uploaded?
- No. Everything runs in your browser, and nothing you type is uploaded. The tool only needs variable definitions, not your data values.