Subtracting case values to create a new variable

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Subtracting case values to create a new variable

Linda Case
Greetings - I have a set of longitudinal data comprised of 26 weeks.  I have
the data formatted with "week" as a variable and have several dependent
variables of interest. One of these is body weight. I would like to
calculate a new variable of change in BW per week (i.e. BWweek1 - Bwweek0;
BWweek2 - Bwweek1, etc).  I have been trying to do this with syntax but seem
to be doing something wrong as it is not working. Can anyone provide me with
a line of syntax to create this variable?



Thanks in advance!



Linda Case



Linda P. Case

AutumnGold Consulting

(217) 586-4864

www.autumngoldconsulting.com

[hidden email] or [hidden email] <mailto:[hidden email]>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Subtracting case values to create a new variable

Richard Ristow
At 12:15 PM 4/25/2008, Linda Case wrote:

>I have a set of longitudinal data comprised of 26 weeks.  I have
>"week" as a variable and several dependent variables of interest.
>One of these is body weight. I would like to calculate a new
>variable of change in BW per week

Assuming that each week's data is a separate SPSS record, function
LAG was invented for this:

COMPUTE BWchange = BW - LAG(BW).

If you have several people in the file, you need extra code to make
sure you don't treat person 2's week 1 as 'following' person 1's week 26.

And if your data isn't organized like this, post again, telling us
how it is laid out.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Subtracting case values to create a new variable

Linda Case
Thanks Richard!

Yes, there are multiple subjects (dogs, actually).  They are identified
under the variable "Name", and so each dog has 26 records (rows), one for
each week.  So, could you tell me how to tell SPSS to delineate between the
individual subjects when I compute BW difference between weeks using the
code that you provided?

Thanks in advance!

Linda

Linda P. Case
AutumnGold Consulting
(217) 586-4864
www.autumngoldconsulting.com
[hidden email] or [hidden email]
-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Richard Ristow
Sent: Friday, April 25, 2008 2:28 PM
To: [hidden email]
Subject: Re: Subtracting case values to create a new variable

At 12:15 PM 4/25/2008, Linda Case wrote:

>I have a set of longitudinal data comprised of 26 weeks.  I have
>"week" as a variable and several dependent variables of interest.
>One of these is body weight. I would like to calculate a new
>variable of change in BW per week

Assuming that each week's data is a separate SPSS record, function
LAG was invented for this:

COMPUTE BWchange = BW - LAG(BW).

If you have several people in the file, you need extra code to make
sure you don't treat person 2's week 1 as 'following' person 1's week 26.

And if your data isn't organized like this, post again, telling us
how it is laid out.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Subtracting case values to create a new variable

Linda Case
In reply to this post by Linda Case
Resending -

Thanks for this code. I tried it and it worked, but as you noted, did not
delineate between subjects. There are multiple subjects (dogs, actually).
They are identified under the variable "Name", and so each dog has 26
records (rows), one for each week.



So, could you tell me how to tell SPSS to delineate between the individual
subjects when I compute BW difference between weeks using the code that you
provided?

Thanks in advance!

Linda

Linda P. Case
AutumnGold Consulting
(217) 586-4864
www.autumngoldconsulting.com
[hidden email] or [hidden email]
-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Richard Ristow
Sent: Friday, April 25, 2008 2:28 PM
To: [hidden email]
Subject: Re: Subtracting case values to create a new variable

At 12:15 PM 4/25/2008, Linda Case wrote:

>I have a set of longitudinal data comprised of 26 weeks.  I have
>"week" as a variable and several dependent variables of interest.
>One of these is body weight. I would like to calculate a new
>variable of change in BW per week

Assuming that each week's data is a separate SPSS record, function
LAG was invented for this:

COMPUTE BWchange = BW - LAG(BW).

If you have several people in the file, you need extra code to make
sure you don't treat person 2's week 1 as 'following' person 1's week 26.

And if your data isn't organized like this, post again, telling us
how it is laid out.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Subtracting case values to create a new variable

Richard Ristow
At 05:25 PM 4/25/2008, Linda Case wrote:

>Thanks for this code. I tried it and it worked, but as you noted,
>did not delineate between subjects. There are multiple subjects
>(dogs, actually). They are identified under the variable "Name", and
>so each dog has 26 records (rows), one for each week.

Apologies for the delay. I'm afraid that, with traveling and work
obligations, I've been essentially off the Net since I first posted.

Here's the easiest way. It requires that the file be sorted by Name
and by week within each Name (and doesn't check the latter). And it
leaves variable New_Dog in the file. If any of this is a problem,
there are somewhat more complex alternatives.  Not tested, but pretty standard:

ADD FILES
   /FILE=*
   /BY      Name
   /FIRST = New_Dog.

DO IF   New_Dog
.  COMPUTE BWchange = $SYSMIS.
ELSE.
.  COMPUTE BWchange = BW - LAG(BW).
END IF.
...................................
P.S. The line
.  COMPUTE BWchange = $SYSMIS.
is technically not necessary: numeric variables are initialized as
system-missing, and stay that way unless assigned a value. Including
it is my programming style: for clarity and reliability of code,
avoid relying on defaults.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD