IF STATEMENT BASED UPON PREVIOUS RECORD

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

IF STATEMENT BASED UPON PREVIOUS RECORD

Matthew Nulle

Hi everyone,

 

I am creating syntax to recode a specific variable with one per group.  It is sorted into groups so the first record of every group is the record that needs to be changed.  Only problem is how do I refer to a previous record and a current record within an if statement. 

 

Here is how I am hoping it works out. 

 

If record 4 var1 <> record 3 var1 then change var7 to sysmis for record 4. 

 

Any help would be incredible.  Thanks,

 

Matt Nulle

 

Reply | Threaded
Open this post in threaded view
|

Re: IF STATEMENT BASED UPON PREVIOUS RECORD

Marta Garcia-Granero
Matthew Nulle wrote:

>
> I am creating syntax to recode a specific variable with one per
> group.  It is sorted into groups so the first record of every group is
> the record that needs to be changed.  Only problem is how do I refer
> to a previous record and a current record within an if statement.
>
>
>
> Here is how I am hoping it works out.
>
>
>
> If record 4 var1 <> record 3 var1 then change var7 to sysmis for
> record 4.
>
>
On the fly (not tested with SPSS by me but it should be OK, or at least
give you an idea of how to deal with the problem): use the lag function

IF var1 <> LAG(var1) var7=$SYSMIS.

See if this works and if not, send a small sample of your data.

Regards,
Marta GarcĂ­a-Granero

--
For miscellaneous statistical stuff, visit:
http://gjyp.nl/marta/

=====================
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: IF STATEMENT BASED UPON PREVIOUS RECORD

Art Kendall
In reply to this post by Matthew Nulle
if var1 ne lag(var1) var7 = -1.
missing values var7 (-1).
value labels 'user set to missing value due to change in var1'.

I try to avoid ever explicitly setting a value to sysmis.  The distinction between user missing and system missing gives SPSS a major advantage in developing and debugging syntax.
It is good practice to reserve sysmis for occasions when the program cannot follow your instructions.
In this instance the value is missing for a reason the user knows.

This same kind of lag can be used to set the count within a group to 1 when counting records within a group.

Art Kendall
Social Research consultants


Matthew Nulle wrote:

Hi everyone,

 

I am creating syntax to recode a specific variable with one per group.  It is sorted into groups so the first record of every group is the record that needs to be changed.  Only problem is how do I refer to a previous record and a current record within an if statement. 

 

Here is how I am hoping it works out. 

 

If record 4 var1 <> record 3 var1 then change var7 to sysmis for record 4. 

 

Any help would be incredible.  Thanks,

 

Matt Nulle

 

Art Kendall
Social Research Consultants