How can I compare non-consecutive variables or ii) Delete variables

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

How can I compare non-consecutive variables or ii) Delete variables

SP
I have data (below) that captures the option number that participants
chose.  Cells equaling -1 are extraneous and I would like to ignore those
instances when I compare adjacent variables.  My goal is to count the
number of switching of options.

V4       V5      V6     V7       V8       V9     V10     V11     V12

2.0 -1.0 1.0 -1.0 3.0 -1.0 2.0 -1.0 1.0

2.0 3.0 -1.0 -1.0 1.0 -1.0 3.0 -1.0 2.0

1.0 2.0 3.0 3.0 -1.0 -1.0 2.0 1.0 -1.0



My syntax is incorrect and I'm getting the error:  LOOP HAS NO EFFECT ON
THIS COMMAND.  I'm not sure what this means? Here's the code below.  I'm
also wondering whether there's a way to delete all the instances of -1 in
each case?  I don't think its possible but can someone verify?



COMPUTE Max_i = NVALID(V4 to V12).

COMPUTE CUR_POS = 1.

COMPUTE NEXT_POS = CUR_POS + 1.

COMPUTE doors_switched = 0.

FORMATS doors_switched(f2.0).

VECTOR VECTORVAR = V4 TO V12.

FORMATS Max_i CUR_POS NEXT_POS (f8.0).



LOOP #i = 1 to (Max_i-1).

DO IF (VECTORVAR(#i) = -1).  /*END THE IF CLAUSE WHEN THE CASE IS -1

   - PRINT / 'VECTORVAR(#i) = ' VECTORVAR(#i).

   - BREAK.

ELSE IF (VECTORVAR(#i+1) ~= -1).  /*COUNT THE SWITCH IF THE TWO ADJACENT
CASES BEING COMPARED IS NOT EQUAL TO -1 WHICH WAS CODED FOR "FADING DOOR
SAVED"

     - DO  IF (VECTORVAR(#i) ~= VECTORVAR(#i+1)).     /*COUNT THE SWITCH.

     - compute doors_switched = doors_switched + 1.  /*COUNT THE SWITCH.

     - ELSE.           /*COUNT THE SWITCH.

     -  compute doors_switched = doors_switched.      /*COUNT THE SWITCH.

     - END IF.

ELSE IF (VECTORVAR(#i+1) = -1).  /*SKIP OVER THE CASE WITH -1 AND COMPARE
WITH THE CURRENT CASE WITH THE FOLLOWING CASE

     - DO  IF (VECTORVAR(#i) ~= VECTORVAR(#i+2)).  /*COUNT THE SWITCH.

 - compute doors_switched = doors_switched + 1.  /*COUNT THE SWITCH.

     - ELSE.
 - compute doors_switched = doors_switched. /*COUNT THE SWITCH.

     - END IF.

END IF.

END LOOP.

EXECUTE.

=====================
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: How can I compare non-consecutive variables or ii) Delete variables

Rich Ulrich
I don't know about your error message, but I will ignore that
because there is a much easier way.

1. Use VarsToCases to write the values per line.  Saving an index will
preserve all possible information, when you ...

2. SELECT IF   (Door ne -1).

3. Use Lag( )  to count the changes.
DO IF   ($CASENO eq 1) or (ID ne lag(ID) .
COMPUTE  dchange= 0.
ELSE .
IF  (door ne LAG(door))  dchange= dchange+1.
END IF.

4. Use AGGREGATE to get the max value for dchange.

--
Rich Ulrich

> Date: Fri, 26 Apr 2013 17:54:29 -0400

> From: [hidden email]
> Subject: How can I compare non-consecutive variables or ii) Delete variables
> To: [hidden email]
>
> I have data (below) that captures the option number that participants
> chose. Cells equaling -1 are extraneous and I would like to ignore those
> instances when I compare adjacent variables. My goal is to count the
> number of switching of options.
>
> V4 V5 V6 V7 V8 V9 V10 V11 V12
>
> 2.0 -1.0 1.0 -1.0 3.0 -1.0 2.0 -1.0 1.0
>
> 2.0 3.0 -1.0 -1.0 1.0 -1.0 3.0 -1.0 2.0
>
> 1.0 2.0 3.0 3.0 -1.0 -1.0 2.0 1.0 -1.0
>
>
>
> My syntax is incorrect and I'm getting the error: LOOP HAS NO EFFECT ON
> THIS COMMAND. I'm not sure what this means? Here's the code below. I'm
> also wondering whether there's a way to delete all the instances of -1 in
> each case? I don't think its possible but can someone verify?
...
Reply | Threaded
Open this post in threaded view
|

Re: How can I compare non-consecutive variables or ii) Delete variables

David Marso
Administrator
In reply to this post by SP
When posting this sort of a question ALWAYS post what the data should look like after the transformations.  I will typically NOT bother to debug other people's code since I can write a solution from ground 0 in a fraction of the time it would take to figure out someone else's logic and modify it.
Same for verbal explanations. Easier to just look at inputs and outputs and slam together   QAD functional code rather than correlate the verbal description to the data.
My first impulse is similar to Rich's VARSTOCASES clobber the unwanted rows (-1) then LAG.
Maybe followed by a CASESTOVARS to put everything back in one case (row) at the end.
--
S. P. wrote
I have data (below) that captures the option number that participants
chose.  Cells equaling -1 are extraneous and I would like to ignore those
instances when I compare adjacent variables.  My goal is to count the
number of switching of options.

V4       V5      V6     V7       V8       V9     V10     V11     V12

2.0 -1.0 1.0 -1.0 3.0 -1.0 2.0 -1.0 1.0

2.0 3.0 -1.0 -1.0 1.0 -1.0 3.0 -1.0 2.0

1.0 2.0 3.0 3.0 -1.0 -1.0 2.0 1.0 -1.0



My syntax is incorrect and I'm getting the error:  LOOP HAS NO EFFECT ON
THIS COMMAND.  I'm not sure what this means? Here's the code below.  I'm
also wondering whether there's a way to delete all the instances of -1 in
each case?  I don't think its possible but can someone verify?



COMPUTE Max_i = NVALID(V4 to V12).

COMPUTE CUR_POS = 1.

COMPUTE NEXT_POS = CUR_POS + 1.

COMPUTE doors_switched = 0.

FORMATS doors_switched(f2.0).

VECTOR VECTORVAR = V4 TO V12.

FORMATS Max_i CUR_POS NEXT_POS (f8.0).



LOOP #i = 1 to (Max_i-1).

DO IF (VECTORVAR(#i) = -1).  /*END THE IF CLAUSE WHEN THE CASE IS -1

   - PRINT / 'VECTORVAR(#i) = ' VECTORVAR(#i).

   - BREAK.

ELSE IF (VECTORVAR(#i+1) ~= -1).  /*COUNT THE SWITCH IF THE TWO ADJACENT
CASES BEING COMPARED IS NOT EQUAL TO -1 WHICH WAS CODED FOR "FADING DOOR
SAVED"

     - DO  IF (VECTORVAR(#i) ~= VECTORVAR(#i+1)).     /*COUNT THE SWITCH.

     - compute doors_switched = doors_switched + 1.  /*COUNT THE SWITCH.

     - ELSE.           /*COUNT THE SWITCH.

     -  compute doors_switched = doors_switched.      /*COUNT THE SWITCH.

     - END IF.

ELSE IF (VECTORVAR(#i+1) = -1).  /*SKIP OVER THE CASE WITH -1 AND COMPARE
WITH THE CURRENT CASE WITH THE FOLLOWING CASE

     - DO  IF (VECTORVAR(#i) ~= VECTORVAR(#i+2)).  /*COUNT THE SWITCH.

 - compute doors_switched = doors_switched + 1.  /*COUNT THE SWITCH.

     - ELSE.
 - compute doors_switched = doors_switched. /*COUNT THE SWITCH.

     - END IF.

END IF.

END LOOP.

EXECUTE.

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: How can I compare non-consecutive variables or ii) Delete variables

Ruben Geert van den Berg
In reply to this post by SP
If I understand correctly: whenever the within-person value changes, it should be counted as a switch. But variables holding a value of -1 should be treated as if they don't exist in the data? So

-1 2 -1 3 3 2

should be processed as

[nothing][initial value][nothing][switch from 2 to 3][constant][switch from 3 to 2]

and result in switches = 2 ? If so, try the code below. Otherwise, please clarify.

Best,

Ruben

data list free/id.
begin data
1 2 3 4 5 6 7 8 9 10
end data.

do repeat v=v1 to v5.
compute v=tru(rv.uni(1,4)).
if rv.ber(.3)= 1 v=-1.
end repeat.
exe.

compute switches = 0.
do repeat v=v1 to v5/c=1 to 5.
if c = 1 # = v.
if c gt 1 and v ne # and # ne -1 and v ne -1 switches = switches + 1.
if c gt 1 and v ne -1 # = v.
end repeat.
exe.
SP
Reply | Threaded
Open this post in threaded view
|

Re: How can I compare non-consecutive variables or ii) Delete variables

SP
Thank you all for your responses.  I apologize for not reading up on the posting conventions prior to posting.  I want to clarify my example, as per Rubin's request.

2 -1 2 -1 -1 3 3 2 -1

should be processed as

[initial value][nothing][constant][nothing][nothing][switch from 2 to 3][constant][switch from
3 to 2][nothing]

and result in switches = 2.

Does your solution differ from Rich's and David's solution by not removing the -1 cases from the dataset?  
Reply | Threaded
Open this post in threaded view
|

Re: How can I compare non-consecutive variables or ii) Delete variables

Bruce Weaver
Administrator
"Does your solution differ from Rich's and David's solution by not removing the -1 cases from the dataset?"

Ruben's solution does not remove them from the dataset, but it also does not increment the switch counter when it encounters -1.  Try it with your sample line of data:

data list  list / v1 to v9 (9f2.0).
begin data
2 -1 2 -1 -1 3 3 2 -1
end data.

compute switches = 0.
do repeat v = v1 to v9 / c = 1 to 9.
*** On the first loop, just set # = v1.
+  if c eq 1 # = v.
*** After the first loop, increment switch counter if necessary.
+  if c gt 1 and v ne # and # ne -1 and v ne -1 switches = switches + 1.
*** Finally, if v is not missing, set # = the v for this loop, otherwise leave it as is.
+  if c gt 1 and v ne -1 # = v.
end repeat.
*execute.
formats switches(f2.0).
list.

OUTPUT:
v1 v2 v3 v4 v5 v6 v7 v8 v9 switches
 
 2 -1  2 -1 -1  3  3  2 -1     2
 
Number of cases read:  1    Number of cases listed:  1


SP wrote
Thank you all for your responses.  I apologize for not reading up on the posting conventions prior to posting.  I want to clarify my example, as per Rubin's request.

2 -1 2 -1 -1 3 3 2 -1

should be processed as

[initial value][nothing][constant][nothing][nothing][switch from 2 to 3][constant][switch from
3 to 2][nothing]

and result in switches = 2.

Does your solution differ from Rich's and David's solution by not removing the -1 cases from the dataset?
--
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: How can I compare non-consecutive variables or ii) Delete variables

Ruben Geert van den Berg
In reply to this post by SP
Dear SP,

If you just run all the solutions you were offered you can see for yourself whether/how they differ, right?

Best,

Ruben
SP
Reply | Threaded
Open this post in threaded view
|

Re: How can I compare non-consecutive variables or ii) Delete variables

SP
FYI, I tried Bruce Weaver's method and it worked perfectly.  
Thanks for everyone's help.