How to create a transform formula with an if then formula

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash


How to create a transform formula with an if then formula

Postby Jan » Tue Oct 23, 2007 8:04 pm

I want to create a new variable with a formula that computes the difference between respondent(row) 1 and 2, 3 and 4, 5 and 6, etc.

The odd numbers are labelled with 1 and the even with 2.

Which formula can I use?

Data new variable
1:abcd (a-e)
2: efgh (a-e)
1: ijkl (i-m)
2: mnop (i-m)

Thanks in advance,

Posts: 233
Joined: Tue Aug 07, 2007 11:48 am

Postby Smash » Tue Oct 23, 2007 10:11 pm

You can use LAG function, but it could be used only for previouse rows, so for your rows number 2.
Let say variables are: X1 X2 X3 X4

Code: Select all

compute diff1=lag(X1)-X1.
But because you can't do it for pairs, I suggest you to create one order variable and double use lag function. One for calculation one for copying the results.

Simple code, sorry striaght from the head, so it may not work 100% ok :twisted:

Code: Select all

compute nr=$casenum.
compute diff1=lag(X1)-X1.
sort cases by nr (d).
if (mod(nr,2)=1) diff1=lag(diff1).
sort cases by nr (a).

Who is online

Users browsing this forum: No registered users and 1 guest