IBM SPSS Statistics Syntax

Topic
  • 17 replies
  • Latest Post - ‏2014-06-05T12:25:37Z by Coop197823
Coop197823
Coop197823
16 Posts

Pinned topic Find Consecutive numbers in a row

‏2014-06-04T19:56:15Z |

I have a row of 15 columns.  I need to find out the highest number of consecutive values within that row of 15 variables.  Is this possible within SPSS?  It loosk like Count Occurrences if is the way to go, but the consecutive part is messing me up.

 

Thanks!

  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T21:16:17Z  

    Count is not going to do anything for consecutive values.  The following code will do it.  It assumes that a sequence should count 1,2 as a 2 rather than as one repeat.

     

    data list list/x1 to x15(5f2.0).
    begin data
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    1 3 4 2 1 2 2 2 2 2 2 2 2 2 2
    1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    0 1 2 2 2 3 3 3 3 3 3 3 3 3 3
    end data.

    vector v = x1 to x15.
    compute #run = 1.
    compute maxrun = 1.
    loop #i = 2 to 15.
         do if v(#i) eq v(#i-1) + 1.
            compute #run = #run + 1.
            compute maxrun = max(maxrun, #run).
         else.
            compute #run = 1.
         end if.
    end loop.
    exec.

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T21:44:56Z  
    • JonPeck
    • ‏2014-06-04T21:16:17Z

    Count is not going to do anything for consecutive values.  The following code will do it.  It assumes that a sequence should count 1,2 as a 2 rather than as one repeat.

     

    data list list/x1 to x15(5f2.0).
    begin data
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    1 3 4 2 1 2 2 2 2 2 2 2 2 2 2
    1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
    0 1 2 2 2 3 3 3 3 3 3 3 3 3 3
    end data.

    vector v = x1 to x15.
    compute #run = 1.
    compute maxrun = 1.
    loop #i = 2 to 15.
         do if v(#i) eq v(#i-1) + 1.
            compute #run = #run + 1.
            compute maxrun = max(maxrun, #run).
         else.
            compute #run = 1.
         end if.
    end loop.
    exec.

    As I am still new to spss syntax, I have some questions.  Say my variable names are x1 to x15 and the values are just 1s and zeros.  I am having some difficulty taking this syntax that is based on the data you are defining versus applying the syntax versus my real dataset (ie without the begin data/ end data block).  How would the syntax look without that block?

    Clinton

  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T21:56:22Z  

    As I am still new to spss syntax, I have some questions.  Say my variable names are x1 to x15 and the values are just 1s and zeros.  I am having some difficulty taking this syntax that is based on the data you are defining versus applying the syntax versus my real dataset (ie without the begin data/ end data block).  How would the syntax look without that block?

    Clinton

    No change.  The begin/end data was just to create an example.  Just start with the vector command.

    Your question, though, makes me wonder whether what you want is different from what I thought.  Are you just

    looking for runs of identical values in the data?  For example,

    0 0 1 1 1

    should produce 3.

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T22:12:24Z  
    • JonPeck
    • ‏2014-06-04T21:56:22Z

    No change.  The begin/end data was just to create an example.  Just start with the vector command.

    Your question, though, makes me wonder whether what you want is different from what I thought.  Are you just

    looking for runs of identical values in the data?  For example,

    0 0 1 1 1

    should produce 3.

    yep that is it.  I should have explained my problem better.  When I run:   vector v = x1 to x15.  I get an error:

     

    Warning # 513

    The subscript in a vector reference is missing or otherwise invalid. The

    subscript must be a positive integer and must not be greater than the length

    of the vector. The result has been set to the system-missing value.

    Command line: 142 Current case: 1 Current splitfile group: 1

     

    Here is my code:

    VECTOR V = GEN2008 to GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = GEN2008 to GEN2013.
         DO IF V(#i) EQ V(#i-1) + 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXECUTE.

     

     

     

     

  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T22:20:40Z  

    yep that is it.  I should have explained my problem better.  When I run:   vector v = x1 to x15.  I get an error:

     

    Warning # 513

    The subscript in a vector reference is missing or otherwise invalid. The

    subscript must be a positive integer and must not be greater than the length

    of the vector. The result has been set to the system-missing value.

    Command line: 142 Current case: 1 Current splitfile group: 1

     

    Here is my code:

    VECTOR V = GEN2008 to GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = GEN2008 to GEN2013.
         DO IF V(#i) EQ V(#i-1) + 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXECUTE.

     

     

     

     

    It is good that you posted the code.  The LOOP line should be using numbers,  not names, i.e.,

    LOOP #i = 2 TO 15.

    The VECTOR command takes care of mapping the index to the names.

    But to count runs, use this code.

    vector v = GEN2008 to GEN2013.
    compute #run = 1.
    compute maxrun = 1.
    loop #i = 2 to 15.
         do if v(#i) eq v(#i-1).
            compute #run = #run + 1.
            compute maxrun = max(maxrun, #run).
         else.
            compute #run = 1.
         end if.
    end loop.
    exec.

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T22:32:36Z  
    • JonPeck
    • ‏2014-06-04T22:20:40Z

    It is good that you posted the code.  The LOOP line should be using numbers,  not names, i.e.,

    LOOP #i = 2 TO 15.

    The VECTOR command takes care of mapping the index to the names.

    But to count runs, use this code.

    vector v = GEN2008 to GEN2013.
    compute #run = 1.
    compute maxrun = 1.
    loop #i = 2 to 15.
         do if v(#i) eq v(#i-1).
            compute #run = #run + 1.
            compute maxrun = max(maxrun, #run).
         else.
            compute #run = 1.
         end if.
    end loop.
    exec.

    Got it working without warnings/errors, but it is only returning either a 1 or 2 even though in some cases the consec is way over 2.  Any thoughts on this?

  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T22:38:59Z  

    Got it working without warnings/errors, but it is only returning either a 1 or 2 even though in some cases the consec is way over 2.  Any thoughts on this?

    I suspect that your variables are not in the order required by the VECTOR command.  Using TO refers to variables in the order they appear in the file.  So, with the code above, if the variable order was

    GEN2008 GEN2013 GEN2009 ...

    the loop would only run for two variables.  You can reorder the variables in the Data Editor or list the TO in file order (assuming there are no others in between).

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T22:48:29Z  
    • JonPeck
    • ‏2014-06-04T22:38:59Z

    I suspect that your variables are not in the order required by the VECTOR command.  Using TO refers to variables in the order they appear in the file.  So, with the code above, if the variable order was

    GEN2008 GEN2013 GEN2009 ...

    the loop would only run for two variables.  You can reorder the variables in the Data Editor or list the TO in file order (assuming there are no others in between).

    I am trying this just on a few variable columns first before I go on the full 15, so here is what the data looks like with the maxrun var:

     

     

    GEN2008 GEN2009 GEN2010 GEN2011 GEN2012 GEN2013 MAXRUN
    1 1 1 1 1 1 1
    1 0 0 0 1 0 2
    1 1 0 1 1 0 2
    1 0 1 0 1 0 2
    1 1 1 1 1 1 1
    1 0 0 0 1 0 2
    1 0 1 0 1 0 2
    1 0 0 0 1 0 2
    1 0 0 0 1 0 2
    1 0 0 0 1 0 2
    1 0 0 0 1 0 2
    0 0 0 1 1 0 2
    1 1 0 0 0 0 2
    1 1 1 1 1 0 2
    1 1 1 1 1 1 2
    1 1 1 1 1 1 2
    1 0 0 0 1 0 2
    1 1 1 1 1 0 1

     

    As long as it reads forward through the rows, they are in the right order.  Also, I have changed the  LOOP #i = 2 to 15. to LOOP #i = 2 to 6.

     

  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T22:54:40Z  

    I am trying this just on a few variable columns first before I go on the full 15, so here is what the data looks like with the maxrun var:

     

     

    GEN2008 GEN2009 GEN2010 GEN2011 GEN2012 GEN2013 MAXRUN
    1 1 1 1 1 1 1
    1 0 0 0 1 0 2
    1 1 0 1 1 0 2
    1 0 1 0 1 0 2
    1 1 1 1 1 1 1
    1 0 0 0 1 0 2
    1 0 1 0 1 0 2
    1 0 0 0 1 0 2
    1 0 0 0 1 0 2
    1 0 0 0 1 0 2
    1 0 0 0 1 0 2
    0 0 0 1 1 0 2
    1 1 0 0 0 0 2
    1 1 1 1 1 0 2
    1 1 1 1 1 1 2
    1 1 1 1 1 1 2
    1 0 0 0 1 0 2
    1 1 1 1 1 0 1

     

    As long as it reads forward through the rows, they are in the right order.  Also, I have changed the  LOOP #i = 2 to 15. to LOOP #i = 2 to 6.

     

    I get the right answers running this.

    data list list/gen2008 gen2009 gend010 gen2011 gen2012 gen2013.
    begin data
    1 1 1 1 1 1
    0 1 0 1 0 1
    end data.
    vector v = GEN2008 to GEN2013.
    compute #run = 1.
    compute maxrun = 1.
    loop #i = 2 to 6.
         do if v(#i) eq v(#i-1).
            compute #run = #run + 1.
            compute maxrun = max(maxrun, #run).
         else.
            compute #run = 1.
         end if.
    end loop.
    exec.

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T23:04:24Z  
    • JonPeck
    • ‏2014-06-04T22:54:40Z

    I get the right answers running this.

    data list list/gen2008 gen2009 gend010 gen2011 gen2012 gen2013.
    begin data
    1 1 1 1 1 1
    0 1 0 1 0 1
    end data.
    vector v = GEN2008 to GEN2013.
    compute #run = 1.
    compute maxrun = 1.
    loop #i = 2 to 6.
         do if v(#i) eq v(#i-1).
            compute #run = #run + 1.
            compute maxrun = max(maxrun, #run).
         else.
            compute #run = 1.
         end if.
    end loop.
    exec.

    And when I run it, I get this  There has to be something I am doing wrong!

     

    GEN2008 GEN009 GEN2010 GEN2011 GEN2012 GEN2013 MAXRUN
    1 1 1 1 1 1 1
    0 1 0 1 0 1 2

     

     

    DATA LIST LIST/GEN2008 GEN009 GEN2010 GEN2011 GEN2012 GEN2013.
    begin data
    1 1 1 1 1 1
    0 1 0 1 0 1
    end data.
    VECTOR V = GEN2008 TO GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = 2 TO 6.
         DO IF V(#i) EQ V(#i-1) + 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXEC.

     

    maybe what I am expecting it to do is not what it is doing.  I am looking for the most consecutive numbers that are not zero (ie 111111 = 6, 010101 = 1, 110001 = 2, 000111 = 3, etc)

  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T23:05:30Z  

    And when I run it, I get this  There has to be something I am doing wrong!

     

    GEN2008 GEN009 GEN2010 GEN2011 GEN2012 GEN2013 MAXRUN
    1 1 1 1 1 1 1
    0 1 0 1 0 1 2

     

     

    DATA LIST LIST/GEN2008 GEN009 GEN2010 GEN2011 GEN2012 GEN2013.
    begin data
    1 1 1 1 1 1
    0 1 0 1 0 1
    end data.
    VECTOR V = GEN2008 TO GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = 2 TO 6.
         DO IF V(#i) EQ V(#i-1) + 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXEC.

     

    maybe what I am expecting it to do is not what it is doing.  I am looking for the most consecutive numbers that are not zero (ie 111111 = 6, 010101 = 1, 110001 = 2, 000111 = 3, etc)

    Please post an extract of your dataset and the exact syntax you are running.  There are some possibilities, but I'd rather not speculate.

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T23:09:15Z  
    • JonPeck
    • ‏2014-06-04T23:05:30Z

    Please post an extract of your dataset and the exact syntax you are running.  There are some possibilities, but I'd rather not speculate.

    EXACT CODE:

    VECTOR V = GEN2008 TO GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = 2 TO 6.
         DO IF V(#i) EQ V(#i-1) + 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXECUTE.

     

     

    SNIPPET OF DATA:

     

     

    GEN2008
    GEN2009 GEN2010 GEN2011 GEN2012 GEN2013 MAXRUN DESIRED MAXRUN
    1 1 1 1 1 1 1 6
    1 0 0 0 1 0 2 1
    1 1 0 1 1 0 2 2
    1 0 1 0 1 0 2 1
    1 1 1 1 1 1 1 6
    1 0 0 0 1 0 2 1
    1 0 1 0 1 0 2 1
    1 0 0 0 1 0 2 1
    1 0 0 0 1 0 2 1
    1 0 0 0 1 0 2 1
    1 0 0 0 1 0 2 1
    0 0 0 1 1 0 2 2
    1 1 0 0 0 0 2 2
    1 1 1 1 1 0 2 5
    1 1 1 1 1 1 2 6
    1 1 1 1 1 1 2 6
    1 0 0 0 1 0 2 1
    1 1 1 1 1 0 1
     
     
     
     
    5
  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T23:50:56Z  

    EXACT CODE:

    VECTOR V = GEN2008 TO GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = 2 TO 6.
         DO IF V(#i) EQ V(#i-1) + 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXECUTE.

     

     

    SNIPPET OF DATA:

     

     

    GEN2008
    GEN2009 GEN2010 GEN2011 GEN2012 GEN2013 MAXRUN DESIRED MAXRUN
    1 1 1 1 1 1 1 6
    1 0 0 0 1 0 2 1
    1 1 0 1 1 0 2 2
    1 0 1 0 1 0 2 1
    1 1 1 1 1 1 1 6
    1 0 0 0 1 0 2 1
    1 0 1 0 1 0 2 1
    1 0 0 0 1 0 2 1
    1 0 0 0 1 0 2 1
    1 0 0 0 1 0 2 1
    1 0 0 0 1 0 2 1
    0 0 0 1 1 0 2 2
    1 1 0 0 0 0 2 2
    1 1 1 1 1 0 2 5
    1 1 1 1 1 1 2 6
    1 1 1 1 1 1 2 6
    1 0 0 0 1 0 2 1
    1 1 1 1 1 0 1
     
     
     
     
    5

    No, please attach a sav file.  Just a list is not enough to identify the problem.

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-04T23:59:18Z  
    • JonPeck
    • ‏2014-06-04T23:50:56Z

    No, please attach a sav file.  Just a list is not enough to identify the problem.

    Here is the file with 100 cases.  I think I found part of the problem...I removed the +1 from the last part of DO IF V(#i) EQ V(#i-1)+1.  And that gave me the highest consecutive run of both the 1s or the 0s.  So now if I can get the highest consecutive runs of 1s then I will be set!

     

     

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-05T00:22:11Z  

    Here is the file with 100 cases.  I think I found part of the problem...I removed the +1 from the last part of DO IF V(#i) EQ V(#i-1)+1.  And that gave me the highest consecutive run of both the 1s or the 0s.  So now if I can get the highest consecutive runs of 1s then I will be set!

     

     

    SOLVED with one caveat:

     

    VECTOR V = GEN2008 TO GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = 2 TO 6.
         DO IF V(#i) EQ V(#i-1) and V(#i) EQ 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXECUTE.

     

     

    if all rows are zeros, then the maxrun cell would be 1.  is there a way to change this to 0 is all columns in that range are zero?

  • JonPeck
    JonPeck
    269 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-05T03:19:03Z  

    SOLVED with one caveat:

     

    VECTOR V = GEN2008 TO GEN2013.
    COMPUTE #RUN = 1.
    COMPUTE MAXRUN = 1.
    LOOP #i = 2 TO 6.
         DO IF V(#i) EQ V(#i-1) and V(#i) EQ 1.
            COMPUTE #RUN = #RUN + 1.
            COMPUTE MAXRUN = MAX(MAXRUN, #RUN).
         ELSE.
            COMPUTE #RUN = 1.
         END IF.
    END LOOP.
    EXECUTE.

     

     

    if all rows are zeros, then the maxrun cell would be 1.  is there a way to change this to 0 is all columns in that range are zero?

    1) I see that  you were not using the revised code I posted that just counted runs.  It did not have the  "+1" in it.

    2) Are you saying that you only want to count runs of 1's and ignore runs of 0's?  I didn't see that mentioned in the original problem statement.

  • Coop197823
    Coop197823
    16 Posts

    Re: Find Consecutive numbers in a row

    ‏2014-06-05T12:25:37Z  
    • JonPeck
    • ‏2014-06-05T03:19:03Z

    1) I see that  you were not using the revised code I posted that just counted runs.  It did not have the  "+1" in it.

    2) Are you saying that you only want to count runs of 1's and ignore runs of 0's?  I didn't see that mentioned in the original problem statement.

    Thanks Jon for the help!  I made a work around if all cases are zero and just added an if statement that if all variables are zero then maxrun would be zero. 

     

    I guess I need to be more specific in the future when I am outlining my question!  Sorry about that and thanks again!