Next Upcoming Google+ Hangout: Tuesday, August 27 @ 7PM (CST) - To Participate CLICK HERE

Help Me, Help You...
This form does not yet contain any fields.
    « Moderating Effects with Seemingly Uncorrelated Variables | Main | How to make SPSS produce all tables in APA format automatically! »
    Monday
    Jun202011

    Using Syntax to Assign 'Variable Labels' and 'Value Labels' in SPSS


    Preparing a dataset for analysis is an arduous process. Besides recoding and cleaning variables, a diligent data analyst also must assign variable labels and value labels, unless they choose to wait until after your output is exported to Microsoft Word. Unfortunately, that option only leaves additional opportunity for error and confusion, not to mention the inefficiency of editing tables in Microsoft Word. Who among us have not been frustrated while wrestling with Microsoft Word?

    When used in conjunction with the customizable SPSS table "Looks" function, formatting your variable labels and value labels can make your SPSS results tables nearly ready for publication, immediately after analysis! Fortunately, SPSS syntax offers a fairly straightforward method for assigning proper labels to both your variable labels and value labels.

    For those of you unsure about the distinction between the two:

    Variable Labels: Variable labels are composed of a few words that describe what a variable represents. If the variable labels are properly formatted in SPSS, they will show in output tables and graphs, instead of variable names.

    Value Labels: Value labels are labels for coded variables in our dataset. For example, "Gender" may be coded 0 (Males) and 1 (Females).




    The screenshot below shows an example SPSS dataset I created for demonstration purposes (as you can see at the bottom of the screenshot, we are seeing the "variable view", as opposed to "data view". To review, "data view" is used for editing the actual data, whereas "variable view" is used for editing the attributes of the variables (such as number of decimal places allowed, type of variable, the variable name, variable label, and value label). In our example below, neither the variable labels (1) nor the value labels (2) have been assigned for any of our four example variables.

    Variable View SPSS No Variable/Value Labels

    One way to rectify this problem would be to:


    1. Click in the field under "Label" for each variable and simply type in a label.

    2. Click in the field under "Value" (on the right side of the field, click on the button that appears when you click in the field initially; see below left)

    3. Enter the current number code that is assigned with the label that you desire for each code (clicking "add" between each code) in the dialogue that appears (see below right).


    SPSS value label screen exampleSPSS Value Column example

    That would work fine if you only have a couple of variables, However, what if you have 10 variables, or 20, or 100... or 1000? Obviously, this can quickly turn into a ridiculously long process. This is when syntax makes things MUCH easier!

    Here are the steps to assign variable labels:


    1. Open a new syntax window by clicking through the following menu path (see below): File->New->Syntax.

    2. Type the command "VARIABLE LABELS" (be careful of spelling).

    3. On the next line (new line not required, but recommended), first type the name of the variable you want to assign a label to (in my example, the variable is "Example1"; see below).

    4. On the same line as the variable name, insert a space, followed by a "single quote" (not a double quote/quotation mark), followed by whatever text you'd like to assign as the variable label for that variable, followed by another "single quote", and finally a period.


    NOTE: if that is all you wish to do, start a new line and type EXECUTE, followed by another period; If you want to also assign value labels, as we will here, you can save the EXECUTE until the end.

    SPSS Syntax screen example 1

    Here are the steps to assign value labels (in the same syntax window):


    1. Type the command "VALUE LABELS" (be careful of spelling).

    2. On the next line (new line not required, but recommended), type the name of the variable you want to assign a value labels to (in my example, the variable is "Example1"; see below).

    3. On the next line (new line not required, but recommended), type the number code that is currently in your data (to which you want to assign labels; in my example, the first code is 1), followed by a space...

    4. On the same line as the variable name, insert a space, followed by a "single quote", followed by the label you wish to assign to that numeric code, followed by another "single quote".

    5. On a new line, type the command EXECUTE, followed by a period, and then "run" the syntax.


    NOTE: repeat step 2 &3 for each numeric code that exists for that variable; Insert a period after the last code is entered for that variable.

    NOTE: To run the syntax, highlight the portion you wish to run AND THEN click on the "green play button" in the tool bar OR click through the following menu path "Run-> Selection" OR press the keyboard shortcut ("Command+r" [for mac]; "Control+r" [for PC]).


    SPSS Syntax screen example 2

    While this may be useful, doing this with only one variable doesn't offer that much improvement over the "point and click" method. However, when you apply this technique to transform several variables simultaneously, the time saved really begins to accumulate (see below).

    SPSS Syntax screen example 3

    There are a few things to pay particular attention to when working with multiple variables at once:


    • Be sure to insert a period after every line in the variable label command (as pictured above).

    • When assigning value labels, a "variable TO variable" format can be used (see above), instead of listing each variable name separately, as long as the variables are in consecutive order in the dataset (and you want to assign the same labels to all of those variables). IF THEY ARE NOT in consecutive order, you simply need to list each of the names separately.

    • If you have want to assign different sets of value labels to various variables, you simply need to separate each set of value labels with a forward slash ( / ), and begin with the new variable name on the next line (see below).



    SPSS Syntax screen example 4

    After you run your syntax, you should now see the information you chose populating the "Label" and "Value" columns of your dataset (seen below).

    SPSS data sheet screen example number 2

    NOTE: If you also want to specify what codes in your data signify that the data is missing ("Missing" column [see above]), add the following commands to your syntax:

    To assign for a single variable:

    MISSING VALUES

    Example (999).

    EXECUTE.

    To assign for multiple variables:

    MISSING VALUES

    Example1 to Example4 (999).

    EXECUTE.


    NOTE: I will post links below that will allow you to download sample SPSS syntax files used in this tutorial (download either or both below).

    "SINGLE VARIABLE" EXAMPLE SYNTAX

    "MULTI-VARIABLE" EXAMPLE SYNTAX

    Editorial Note: Stats Make Me Cry is owned and operated by Jeremy J. Taylor. The site offers many free statistical resources (e.g. a blog, SPSS video tutorials, R video tutorials, and a discussion forum), as well as fee-based statistical consulting and dissertation consulting services to individuals from a variety of disciplines all over the world.





    PrintView Printer Friendly Version

    EmailEmail Article to Friend

    Reader Comments (14)

    See also the syntax based SPSS tutorials in section "Survey Analysis Workshop and SPSS" on my website:
    http://surveyresearch.weebly.com/

    April 7, 2012 | Unregistered CommenterJohn F Hall

    Thank you very much! That was helpful.

    March 27, 2013 | Unregistered CommenterKala

    Your welcome Kala!

    March 27, 2013 | Registered CommenterJeremy Taylor

    Thanks for the examples! Something I don't think is covered above, if the values are strings be sure to surround them with single quotes. Example where A is Arbitrary and E is example:

    VALUE LABELS
    field_name
    'A' 'Arbitrary'
    'E' 'Example'.
    EXECUTE.

    April 29, 2013 | Unregistered CommenterJustin

    Great Tip, Justin! Perhaps I'll post a follow-up to this post and mention that (citing you, of course)!

    April 30, 2013 | Registered CommenterJeremy Taylor

    New resources are always welcome, thanks for posting John!

    I just checked-out your site and it seems great! If you'd like, I'll be happy to add it to my ""LINKS" page.

    April 30, 2013 | Registered CommenterJeremy Taylor

    Hi. What I'm trying to do is write syntax that will assign a value label to all missing data for a given variable - however the value for the missing variable will be the same as another. Ex:

    VALUE LABELS
    is_insured
    1 'Yes'
    2 'No'
    2 Missing

    I want the missing values to have the same value as a 'No' answer. For some reason I can't find the right syntax for this. Help anyone? Thanks in advance.

    June 20, 2013 | Unregistered CommenterSoph

    Not a good idea to have the same values. Better to assign a value such as 9 or 99 or -9 and then temporarily recode to 2.

    Missing values
    is_insured (9).
    VALUE LABELS
    is_insured
    1 'Yes'
    2 'No'
    9 'Missing'.

    Then use

    temp.
    Recode is_insured (9 = 2).

    . . but why on earth would you want to do this?

    John Hall

    Email: johnfhall@orange.fr
    Website: www.surveyresearch.weebly.com
    SPSS start page: www.surveyresearch.weebly.com/spss-without-tears.html

    June 21, 2013 | Unregistered CommenterJohn Hall

    I would agree that I would not advise coding missing as "No".

    If you want to label missing with '999', simply use this:

    MISSING VALUES is_insured (999).
    EXECUTE.

    At that point, it isn't necessary to include the missing value in the VALUE LABEL, as the value will recognize as missing and show-up in Tables as "Missing".

    June 23, 2013 | Registered CommenterJeremy Taylor

    say I've already input four value labels, but now another value has appeared that didn't exist before and I need to input another label. is there a way to add a value label without having to re-enter the existing ones?

    July 9, 2013 | Unregistered Commentereli

    Do it in the Data Editor. Click on value cell, click on blue square, add extra label. See the SPSS tutorials on my site.

    July 9, 2013 | Unregistered CommenterJohn Hall

    Eli,

    John is correct that you can absolutely use the Data Editor ("Values" column in "Variable View").

    However, you can also use syntax to do this with the following commands:

    ADD VALUE LABELS
    [YOUR VARIABLE NAME HERE]
    # '[YOUR LABEL HERE]'.
    EXECUTE.

    Replace [YOUR VARIABLE NAME HERE] with your variable name (without the brackets) AND replace # with whatever new number you need to label AND replace [YOUR LABEL HERE] with whatever label you want to assign to that number (again, without the brackets).

    You will notice the syntax is exactly the same, except you insert the word "ADD" at the beginning of the first line!

    July 9, 2013 | Registered CommenterJeremy Taylor

    Jeremy, that bit is exactly what I needed - thanks!!
    eli

    July 9, 2013 | Unregistered Commentereli

    Glad it was helpful, Eli!

    July 10, 2013 | Registered CommenterJeremy Taylor

    PostPost a New Comment

    Enter your information below to add a new comment.
    Author Email (optional):
    Author URL (optional):
    Post:
     
    Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>