George
George

Reputation: 189

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!

Upvotes: 2

Views: 2103

Answers (2)

Jignesh Sutar
Jignesh Sutar

Reputation: 2929

The solution(s) provided by eli-k shall more than suffice however if you have a large dataset then you are evaluating the IF condition 7 x number cases in your dataset and no less, something to be conscious of.

If, however, you use DO IF/END IF then you can achieve the same result with only looping through all the cases just once. On large datasets this could have big implications on processing time.

do if Day_of_Interest=1.
  compute Temperature=Sunday.
else if Day_of_Interest=2.
  compute Temperature=Monday.
else if Day_of_Interest=3.
  compute Temperature=Tuesday.
else if Day_of_Interest=4.
  compute Temperature=Wednesday.
else if Day_of_Interest=5.
  compute Temperature=Thursday.
else if Day_of_Interest=6.
  compute Temperature=Friday.
else if Day_of_Interest=7.
  compute Temperature=Saturday.
end if.

You may have to write more lines of code as above but you need to balance writing compact/parsimonious code vs. processing time. You could take this a step further where you list in the code each DO IF statement in descending frequency of Day_of_Interest. These are in principle some of the techniques used in processing large datasets.

Upvotes: 1

eli-k
eli-k

Reputation: 11350

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.

Upvotes: 1

Related Questions