LEAVE command

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

LEAVE command

Jignesh Sutar
Does anyone have any examples to demonstrate or can explain how the LEAVE command works?

I've come across it when using Identify Duplicate Cases through GUI and have tried to study the generated syntax to understand exactly how it works so that I could potentially put it in use in other computations. But I can't quite understand how it works...

Often I do need to identify duplicate cases and the GUI generated syntax isn't always the most parsimonious so was trying to shrink down the auto generated syntax. MatchSequence and InDupGrp are key variables I need to compute. I like the approach the GUI generated syntax takes (avoids using lags) but I just can't nail exactly what the LEAVE statement does and I've tried removing it and substituting with something else but it always fails.  


data list free /x (f1.0) y(a1).
begin data.
1 A
2 A
3 A
4 A
5 A
5 B
5 B
6 A
6 C
7 A
7 B
7 C
8 A
8 A
8 B
9 A
9 A
9 A
9 B
end data.


* Identify Duplicate Cases.
SORT CASES BY x(A) y(A).
MATCH FILES
  /FILE=*
  /BY x y
  /FIRST=PrimaryFirst
  /LAST=PrimaryLast.
DO IF (PrimaryFirst).
COMPUTE  MatchSequence=1-PrimaryLast.
ELSE.
COMPUTE  MatchSequence=MatchSequence+1.
END IF.
LEAVE  MatchSequence.
FORMATS  MatchSequence (f7).
COMPUTE  InDupGrp=MatchSequence>0.
SORT CASES InDupGrp(D).
MATCH FILES
  /FILE=*
  /DROP=PrimaryFirst /* InDupGrp*/ .
VARIABLE LABELS  PrimaryLast 'Indicator of each last matching case as Primary' MatchSequence
    'Sequential count of matching cases'.
VALUE LABELS  PrimaryLast 0 'Duplicate Case' 1 'Primary Case'.
VARIABLE LEVEL  PrimaryLast (ORDINAL) /MatchSequence (SCALE).
EXECUTE.


Many thanks in advance,
Jignesh
Reply | Threaded
Open this post in threaded view
|

Re: LEAVE command

Richard Ristow
At 02:40 PM 11/7/2014, Jignesh Sutar wrote:

>Does anyone have any examples to demonstrate or can explain how the
>LEAVE command works? I've come across it when using Identify
>Duplicate Cases through GUI.  ...  I like the approach the GUI
>generated syntax takes (avoids using lags) but I just can't nail
>exactly what the LEAVE statement does.

The Command Syntax Reference (CSR, also commonly called the "FM" on
this list) is pretty good on LEAVE; here are the most relevant parts
of the CSR article:

"Normally, the program reinitializes variables each time it prepares
to read a new case. LEAVE suppresses reinitialization and retains the
current value [or, one might say, the previous value] of the
specified variable or variables when the program reads the next case.
Variables named on LEAVE must be new variables that do not already
exist in the active dataset prior to the transformation block that
defines them, but they must be defined in the transformation block
prior to the LEAVE command that specifies them."

Let me expand this a little.

The code in an SPSS transformation program is actually the inside of
an implied loop through the active dataset; the transformation code
is executed once for each record ('case') in the active dataset.

An ordinary SPSS variable does not have 'a value'; it has a separate
value in each case in the file. (Jon Peck refers to these as
'casewise variables', which is a very good term.)

Generally, if you compute a variable, the computation is done
independently for each case, and the value is not at all affected by
any value the variable had in preceding cases. When the code begins
for each new case, any new casewise variable starts with no value --
that is, system-missing.

However, if you LEAVE a variable, it does have a value at the
beginning of the code for a case: the value it had at the end of the
preceding case. (For the first case, if it's numeric it starts with 0
rather than SYSMIS.) Here's the first transformation program from
your example, adding variables "MatchSbefore" and "MatchSafter" so
you can see what values MatchSequence had at the beginning and at the
end of each case; and notice that MatchSbefore for each case has the
same value as MatchSafter from the preceding one.

SORT CASES BY x(A) y(A).
MATCH FILES
   /FILE=*
   /BY x y
   /FIRST=PrimaryFirst
   /LAST=PrimaryLast.

NUMERIC  MatchSequence (f7).
NUMERIC  MatchSbefore
          MatchSafter   (F7).

COMPUTE  MatchSbefore = MatchSequence.

DO IF (PrimaryFirst).
.  COMPUTE  MatchSequence=1-PrimaryLast.
ELSE.
.  COMPUTE  MatchSequence=MatchSequence+1.
END IF.
LEAVE  MatchSequence.
COMPUTE  InDupGrp=MatchSequence>0.

COMPUTE  MatchSafter  = MatchSequence.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-NOV-2014 16:19:08       |
|-----------------------------|---------------------------|
x y PrimaryFirst PrimaryLast MatchSequence MatchSbefore MatchSafter InDupGrp

1 A       1           1               0             0           0        .00
2 A       1           1               0             0           0        .00
3 A       1           1               0             0           0        .00
4 A       1           1               0             0           0        .00
5 A       1           1               0             0           0        .00
5 B       1           0               1             0           1       1.00
5 B       0           1               2             1           2       1.00
6 A       1           1               0             2           0        .00
6 C       1           1               0             0           0        .00
7 A       1           1               0             0           0        .00
7 B       1           1               0             0           0        .00
7 C       1           1               0             0           0        .00
8 A       1           0               1             0           1       1.00
8 A       0           1               2             1           2       1.00
8 B       1           1               0             2           0        .00
9 A       1           0               1             0           1       1.00
9 A       0           0               2             1           2       1.00
9 A       0           1               3             2           3       1.00
9 B       1           1               0             3           0        .00

Number of cases read:  19    Number of cases listed:  19

Any clearer, I hope?
=================================
APPENDIX: Test data, and all code
=================================
data list free /x (f1.0) y(a1).
begin data.
1       A
2       A
3       A
4       A
5       A
5       B
5       B
6       A
6       C
7       A
7       B
7       C
8       A
8       A
8       B
9       A
9       A
9       A
9       B
end data.


* Identify Duplicate Cases.
SORT CASES BY x(A) y(A).
MATCH FILES
   /FILE=*
   /BY x y
   /FIRST=PrimaryFirst
   /LAST=PrimaryLast.

NUMERIC  MatchSequence (f7).
NUMERIC  MatchSbefore
          MatchSafter   (F7).

COMPUTE  MatchSbefore = MatchSequence.

DO IF (PrimaryFirst).
.  COMPUTE  MatchSequence=1-PrimaryLast.
ELSE.
.  COMPUTE  MatchSequence=MatchSequence+1.
END IF.
LEAVE  MatchSequence.
COMPUTE  InDupGrp=MatchSequence>0.

COMPUTE  MatchSafter  = MatchSequence.

LIST.

=====================
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: LEAVE command

Bruce Weaver
Administrator
In reply to this post by Jignesh Sutar
Ruben's website has a short tutorial on it here:

   http://www.spss-tutorials.com/leave/

HTH.


Jignesh Sutar wrote
Does anyone have any examples to demonstrate or can explain how the LEAVE command works?

I've come across it when using Identify Duplicate Cases through GUI and have tried to study the generated syntax to understand exactly how it works so that I could potentially put it in use in other computations. But I can't quite understand how it works...

Often I do need to identify duplicate cases and the GUI generated syntax isn't always the most parsimonious so was trying to shrink down the auto generated syntax. MatchSequence and InDupGrp are key variables I need to compute. I like the approach the GUI generated syntax takes (avoids using lags) but I just can't nail exactly what the LEAVE statement does and I've tried removing it and substituting with something else but it always fails.  


data list free /x (f1.0) y(a1).
begin data.
1 A
2 A
3 A
4 A
5 A
5 B
5 B
6 A
6 C
7 A
7 B
7 C
8 A
8 A
8 B
9 A
9 A
9 A
9 B
end data.


* Identify Duplicate Cases.
SORT CASES BY x(A) y(A).
MATCH FILES
  /FILE=*
  /BY x y
  /FIRST=PrimaryFirst
  /LAST=PrimaryLast.
DO IF (PrimaryFirst).
COMPUTE  MatchSequence=1-PrimaryLast.
ELSE.
COMPUTE  MatchSequence=MatchSequence+1.
END IF.
LEAVE  MatchSequence.
FORMATS  MatchSequence (f7).
COMPUTE  InDupGrp=MatchSequence>0.
SORT CASES InDupGrp(D).
MATCH FILES
  /FILE=*
  /DROP=PrimaryFirst /* InDupGrp*/ .
VARIABLE LABELS  PrimaryLast 'Indicator of each last matching case as Primary' MatchSequence
    'Sequential count of matching cases'.
VALUE LABELS  PrimaryLast 0 'Duplicate Case' 1 'Primary Case'.
VARIABLE LEVEL  PrimaryLast (ORDINAL) /MatchSequence (SCALE).
EXECUTE.


Many thanks in advance,
Jignesh
--
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/).