Search code examples
if-statementspss

Creating a New Variable in SPSS with a Value Equal to Another Variable


I apologize for the confusing title. Essentially, the intended functionality in SPSS is working with the following data file:

Monday     Tuesday     Wednesday     Day of Interest    Temperature on Day of interest
72         78          80            2                    

Here we have three variables and then the temperature for each of those days. The goal is to create a new variable, in the example it's Temperature on Day of Interest, that has the same value as the day it corresponds to (e.g. The second day of the week is a Monday, so we want to temperature on Monday in the new variable column). So the outcome of the syntax should make the data file read:

Monday     Tuesday     Wednesday     Day of Interest    Temperature on Day of interest
72         78          80            2                    72

My thoughts are that I can use an If statement, but I'm not sure on exactly how the syntax would be in SPSS. Here is what I have written:

IF (Day of Interest = 2) Temperature on Day of Interest = $Monday.
IF (Day of Interest = 3) Temperature on Day of Interest = $Tuesday. 

Does anyone happen to know how to properly reference the value in various variables in this manner? I hope this is clear, I'll be able to answer any questions!


Solution

  • Creating some fake data to demonstrate on:

    data list list/Sunday Monday Tuesday Wednesday Thursday Friday Saturday  Day_of_Interest    .
    begin data
    41 42 43 44 45 46 47 2
    51 52 53 54 55 56 57 6
    61 62 63 64 65 66 67 4
    71 72 73 74 75 76 77 7
    81 82 83 84 85 86 87 1
    end data.
    

    Now it is possible of course to work with separate IF statements like this:

    IF (Day_of_Interest = 2) Temperature_on_Day_of_interest = Monday.
    IF (Day_of_Interest = 3) Temperature_on_Day_of_interest = Tuesday.
    IF (Day_of_Interest = 4) Temperature_on_Day_of_interest = Wednesday.
    EXECUTE.
    

    But instead of creating seven separate statements for seven days, you can use a loop this way:

    do repeat Dname=Sunday Monday Tuesday Wednesday Thursday Friday Saturday/Dval=1 2 3 4 5 6 7.
    IF (Day_of_Interest = Dval) Temperature_on_Day_of_interest = Dname.
    end repeat.
    EXECUTE.