Recoding based on the value of next case

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

Recoding based on the value of next case

devoidx
Hey all, I'd like to recode a variable's value (lets say Variable A) from 0 to 1 if the next case also has a value of 1 for variable A...so ideally kinda like:

DO IF (A = 1).
RECODE lag(A) (0=1).
END IF.
EXECUTE.

obviously this doesn't work but explains what I'd like to do...any ideas?
thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

Bruce Weaver
Administrator
How about this?

DO IF (A EQ 1) and ($Casenum GT 1).
- IF LAG(A) EQ 0 LAG(A) = 1.
END IF.


devoidx wrote
Hey all, I'd like to recode a variable's value (lets say Variable A) from 0 to 1 if the next case also has a value of 1 for variable A...so ideally kinda like:

DO IF (A = 1).
RECODE lag(A) (0=1).
END IF.
EXECUTE.

obviously this doesn't work but explains what I'd like to do...any ideas?
thanks.
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

devoidx
Bruce Weaver wrote
How about this?

DO IF (A EQ 1) and ($Casenum GT 1).
- IF LAG(A) EQ 0 LAG(A) = 1.
END IF.
Thanks , tried it but getting the error: "The function appearing on the left side of the assignment operator (equals sign) is not valid in that context."
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

Bruce Weaver
Administrator
Yes, of course.  What was I thinking?  You may need to sort the file first, like this:

COMPUTE CaseNum = $Casenumber.
*EXECUTE. /* May be needed--can't test right now.
SORT CASES by CaseNum (D).
DO IF LAG(A) EQ 1.
- RECODE A (0=1).
END IF.
EXECUTE.
SORT CASES BY CaseNum(A).
 
Does this work as expected?  (No SPSS at home, so I can't test right now.)


devoidx wrote
Bruce Weaver wrote
How about this?

DO IF (A EQ 1) and ($Casenum GT 1).
- IF LAG(A) EQ 0 LAG(A) = 1.
END IF.
Thanks , tried it but getting the error: "The function appearing on the left side of the assignment operator (equals sign) is not valid in that context."
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

Rich Ulrich
In reply to this post by devoidx
The old solution for "next record" was to sort the file into reversed order
and use LAG( ).  I notice that this solution would produce a "1" in every
record after the first occurrence of "1" (in the reversed file).  If that is what
you want - an iterated use of "1" to mark those records - then you could do that.

Here is a procedure without that problem/feature; a program that does not
get much use.   Pick up the next value of A with the program call,

SHIFT VALUES var=A  result= NextA  Lead=1.

Then do what computation you want.  I do notice, that if you run the thing
more than once -- using the simple description that you gave -- you will
get "1" promulgated to a record earlier each time.   SO -- That sort of
file-changing is *terrible*, in principle.  If faced with that circumstance, I
would use a new variable, "NewA", which is assigned the revised value; and
remember to use it instead using the Old A.  Maybe, rename the old A to BadA,
to make sure that it doesn't sneak itself into any future analyses.

--
Rich Ulrich

> Date: Tue, 24 Nov 2015 15:08:46 -0700

> From: [hidden email]
> Subject: Recoding based on the value of next case
> To: [hidden email]
>
> Hey all, I'd like to recode a variable's value (lets say Variable A) from 0
> to 1 if the next case also has a value of 1 for variable A...so ideally
> kinda like:
>
> DO IF (A = 1).
> RECODE lag(A) (0=1).
> END IF.
> EXECUTE.
>
> obviously this doesn't work but explains what I'd like to do...any ideas?
> thanks.
>
===================== 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: Recoding based on the value of next case

devoidx
This post was updated on .
In reply to this post by Bruce Weaver
Yeah using sort to flip the cases and putting the next case, before would work...I was crossing my fingers for a way that i wouldn't need to change my sort order as it is already sorted in a particular way (based on 3 other variables) and it needs to maintain that order. Thanks though! =]

Bruce Weaver wrote
Yes, of course.  What was I thinking?  You may need to sort the file first, like this:

COMPUTE CaseNum = $Casenumber.
*EXECUTE. /* May be needed--can't test right now.
SORT CASES by CaseNum (D).
DO IF LAG(A) EQ 1.
- RECODE A (0=1).
END IF.
EXECUTE.
SORT CASES BY CaseNum(A).
 
Does this work as expected?  (No SPSS at home, so I can't test right now.)


devoidx wrote
Bruce Weaver wrote
How about this?

DO IF (A EQ 1) and ($Casenum GT 1).
- IF LAG(A) EQ 0 LAG(A) = 1.
END IF.
Thanks , tried it but getting the error: "The function appearing on the left side of the assignment operator (equals sign) is not valid in that context."
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

Bruce Weaver
Administrator
In reply to this post by Rich Ulrich
You're right, Rich.  SHIFT VALUES looks like the way to go.


Rich Ulrich wrote
The old solution for "next record" was to sort the file into reversed order
and use LAG( ).  I notice that this solution would produce a "1" in every
record after the first occurrence of "1" (in the reversed file).  If that is what
you want - an iterated use of "1" to mark those records - then you could do that.

Here is a procedure without that problem/feature; a program that does not
get much use.   Pick up the next value of A with the program call,

SHIFT VALUES var=A  result= NextA  Lead=1.

Then do what computation you want.  I do notice, that if you run the thing
more than once -- using the simple description that you gave -- you will
get "1" promulgated to a record earlier each time.   SO -- That sort of
file-changing is *terrible*, in principle.  If faced with that circumstance, I
would use a new variable, "NewA", which is assigned the revised value; and
remember to use it instead using the Old A.  Maybe, rename the old A to BadA,
to make sure that it doesn't sneak itself into any future analyses.

--
Rich Ulrich

> Date: Tue, 24 Nov 2015 15:08:46 -0700
> From: [hidden email]
> Subject: Recoding based on the value of next case
> To: [hidden email]
>
> Hey all, I'd like to recode a variable's value (lets say Variable A) from 0
> to 1 if the next case also has a value of 1 for variable A...so ideally
> kinda like:
>
> DO IF (A = 1).
> RECODE lag(A) (0=1).
> END IF.
> EXECUTE.
>
> obviously this doesn't work but explains what I'd like to do...any ideas?
> thanks.
>
     
=====================
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
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

devoidx
In reply to this post by Rich Ulrich
thanks guys, i'll play around with SHIFT VALUES and report back!

Rich Ulrich wrote
The old solution for "next record" was to sort the file into reversed order
and use LAG( ).  I notice that this solution would produce a "1" in every
record after the first occurrence of "1" (in the reversed file).  If that is what
you want - an iterated use of "1" to mark those records - then you could do that.

Here is a procedure without that problem/feature; a program that does not
get much use.   Pick up the next value of A with the program call,

SHIFT VALUES var=A  result= NextA  Lead=1.

Then do what computation you want.  I do notice, that if you run the thing
more than once -- using the simple description that you gave -- you will
get "1" promulgated to a record earlier each time.   SO -- That sort of
file-changing is *terrible*, in principle.  If faced with that circumstance, I
would use a new variable, "NewA", which is assigned the revised value; and
remember to use it instead using the Old A.  Maybe, rename the old A to BadA,
to make sure that it doesn't sneak itself into any future analyses.

--
Rich Ulrich

> Date: Tue, 24 Nov 2015 15:08:46 -0700
> From: [hidden email]
> Subject: Recoding based on the value of next case
> To: [hidden email]
>
> Hey all, I'd like to recode a variable's value (lets say Variable A) from 0
> to 1 if the next case also has a value of 1 for variable A...so ideally
> kinda like:
>
> DO IF (A = 1).
> RECODE lag(A) (0=1).
> END IF.
> EXECUTE.
>
> obviously this doesn't work but explains what I'd like to do...any ideas?
> thanks.
>
     
=====================
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: Recoding based on the value of next case

Kirill Orlov
In reply to this post by devoidx
Lag() isn't allowed on the left hand side.
Lead function is available not as a transformation function but as a Create Time Series procedure (see in Tranform menu).
Shift Values is also an option.
And, of course, MATRIX is our lifesaver forever.


25.11.2015 2:35, devoidx пишет:
Bruce Weaver wrote
How about this?

DO IF (A EQ 1) and ($Casenum GT 1).
- IF LAG(A) EQ 0 LAG(A) = 1.
END IF.


===================== 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: Recoding based on the value of next case

devoidx
SHIFT VALUES took care of my problem like a champ! thanks for the suggestion!
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

Bruce Weaver
Administrator
How about posting the final solution then, so that folks searching the archives in the future can see it?  ;-)


devoidx wrote
SHIFT VALUES took care of my problem like a champ! thanks for the suggestion!
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Recoding based on the value of next case

devoidx
This post was updated on .
Sure, it's pretty much identical to what Rich suggested though so:

I created newA which is the exact copy of originalA variable and:
SHIFT VALUES variable=originalA  result= NextA  Lead=1.
DO IF (NextA = 1).
RECODE newA (0=1).
END IF.

Bruce Weaver wrote
How about posting the final solution then, so that folks searching the archives in the future can see it?  ;-)