Topic
  • 13 replies
  • Latest Post - ‏2013-07-04T13:58:50Z by redrose888
redrose888
redrose888
100 Posts

Pinned topic conditional aggregation

‏2013-07-02T12:51:13Z |

Dear all,

I would run the followong syntax:

 

compute t = 0.

loop if (sysmis(myvar)).

 AGGREGATE
  /OUTFILE=* MODE = ADDVARIABLES OVERWRITE = YES
  /BREAK=t
  /myvar = SUM(var).

end loop.

 

but  I got error messagen, is there any other method to solve this problem?

 

thanks!

 

 

  • JonPeck
    JonPeck
    269 Posts

    Re: conditional aggregation

    ‏2013-07-03T01:36:02Z  

    What is it you are trying to accomplish here?  AGGREGATE operates on an entire dataset while loop operates on the values in a case, so the two don't go together.

  • redrose888
    redrose888
    100 Posts

    Re: conditional aggregation

    ‏2013-07-03T06:30:34Z  

    Hello Jon,

    I want to run a block of syntax so many times, till a certain variable becomes sysmiss. In this block are some compute,  aggregate and match files commands

    something like this:

    while (~sysmis(myvar)) insert "C:\temp\block.sps"

    whether one can via Python do it?

    thanks!

    baiyun

  • JonPeck
    JonPeck
    269 Posts

    Re: conditional aggregation

    ‏2013-07-03T12:50:48Z  

    Hello Jon,

    I want to run a block of syntax so many times, till a certain variable becomes sysmiss. In this block are some compute,  aggregate and match files commands

    something like this:

    while (~sysmis(myvar)) insert "C:\temp\block.sps"

    whether one can via Python do it?

    thanks!

    baiyun

    Are you saying that you want to run this insert code if any case has a sysmis value for myvar or if all the values are sysmis or if a certain case has a sysmis value?  The sysmis test will run on a per-case value, but you can't execute, say, an AGGREGATE command in the middle of passing the cases, since AGGREGATE works on the whole dataset.

  • redrose888
    redrose888
    100 Posts

    Re: conditional aggregation

    ‏2013-07-03T12:58:31Z  
    • JonPeck
    • ‏2013-07-03T12:50:48Z

    Are you saying that you want to run this insert code if any case has a sysmis value for myvar or if all the values are sysmis or if a certain case has a sysmis value?  The sysmis test will run on a per-case value, but you can't execute, say, an AGGREGATE command in the middle of passing the cases, since AGGREGATE works on the whole dataset.

    yes, if any case has a sysmis value for myvar

    at the beginning myvar has some valid value, after do block.sps many times, myvar becomes sysmis

    i could do this with a macro, like

     

    DEFINE !doit(loopnr = !cmdend)

    !DO !cnt=1 !TO !loopnr

    !repeat

    !DOEND

    !ENDDEFINE.

     

    !doit loopnr = 3.

    but i have to check output everytime, I want to do this automatically.

  • JonPeck
    JonPeck
    269 Posts

    Re: conditional aggregation

    ‏2013-07-03T13:17:34Z  

    yes, if any case has a sysmis value for myvar

    at the beginning myvar has some valid value, after do block.sps many times, myvar becomes sysmis

    i could do this with a macro, like

     

    DEFINE !doit(loopnr = !cmdend)

    !DO !cnt=1 !TO !loopnr

    !repeat

    !DOEND

    !ENDDEFINE.

     

    !doit loopnr = 3.

    but i have to check output everytime, I want to do this automatically.

    Here is an example of how you might do this.  Annotations below.

    compute z = $sysmis.
    begin program.
    import spss
    while True:
        curs = spss.Cursor([10])
        values = curs.fetchall()
        curs.close()
        if not (None,) in values:
           break
        spss.Submit("""compute z = rv.uniform(0,1).""")
    end program.
     

    z is the 11th variable in this dataset.  First it gets a cursor object and reads all the values for index 10 (zero based).
    If any value is None, i.e., sysmis, it Submits some syntax, which could be an insert command, and repeats the process.

    Since fetchall returns a tuple containing one element for each case because of the definition of this cursor, the test is whether a tuple with one element, i.e. (None,) , is in any of the cases.

     

    HTH,

    Jon

  • redrose888
    redrose888
    100 Posts

    Re: conditional aggregation

    ‏2013-07-03T13:41:44Z  
    • JonPeck
    • ‏2013-07-03T13:17:34Z

    Here is an example of how you might do this.  Annotations below.

    compute z = $sysmis.
    begin program.
    import spss
    while True:
        curs = spss.Cursor([10])
        values = curs.fetchall()
        curs.close()
        if not (None,) in values:
           break
        spss.Submit("""compute z = rv.uniform(0,1).""")
    end program.
     

    z is the 11th variable in this dataset.  First it gets a cursor object and reads all the values for index 10 (zero based).
    If any value is None, i.e., sysmis, it Submits some syntax, which could be an insert command, and repeats the process.

    Since fetchall returns a tuple containing one element for each case because of the definition of this cursor, the test is whether a tuple with one element, i.e. (None,) , is in any of the cases.

     

    HTH,

    Jon

    thanks Jon!

    you wrote :

    If any value is None, i.e., sysmis, it Submits some syntax

    block.sps should run till myvar becomes sysmis completely

    I ran

     

    get file = "C:\temp\test.sav".

    begin program.
    import spss
    while True:
        curs = spss.Cursor([12])
        values = curs.fetchall()
        curs.close()
        if not (None,) in values:
           break
        spss.Submit("INSERT FILE='c:/temp/block.sps'.")
    end program.

    spss runs non-stop?

  • JonPeck
    JonPeck
    269 Posts

    Re: conditional aggregation

    ‏2013-07-03T14:17:42Z  

    thanks Jon!

    you wrote :

    If any value is None, i.e., sysmis, it Submits some syntax

    block.sps should run till myvar becomes sysmis completely

    I ran

     

    get file = "C:\temp\test.sav".

    begin program.
    import spss
    while True:
        curs = spss.Cursor([12])
        values = curs.fetchall()
        curs.close()
        if not (None,) in values:
           break
        spss.Submit("INSERT FILE='c:/temp/block.sps'.")
    end program.

    spss runs non-stop?

    That loop will run until there are no sysmis values.  You might want to insert a DESCRIPTIVES command to see what is happening to the test variable, or just run the loop once and look in the DE.

  • redrose888
    redrose888
    100 Posts

    Re: conditional aggregation

    ‏2013-07-03T14:28:26Z  
    • JonPeck
    • ‏2013-07-03T14:17:42Z

    That loop will run until there are no sysmis values.  You might want to insert a DESCRIPTIVES command to see what is happening to the test variable, or just run the loop once and look in the DE.

    I understand now why spss ran non-stop! what I want is the reverse direction!

    i want: That loop will run until there are only sysmis values!

    should I write if (None,) in values: instead if not (None,) ?

  • JonPeck
    JonPeck
    269 Posts

    Re: conditional aggregation

    ‏2013-07-03T14:41:29Z  

    I understand now why spss ran non-stop! what I want is the reverse direction!

    i want: That loop will run until there are only sysmis values!

    should I write if (None,) in values: instead if not (None,) ?

    yes

  • redrose888
    redrose888
    100 Posts

    Re: conditional aggregation

    ‏2013-07-04T10:03:28Z  
    • JonPeck
    • ‏2013-07-03T14:41:29Z

    yes

    Hello Jon

    i let the following lines run:

    get file = "C:\temp\test1.sav".


    begin program.
    import spss
    while True:
        curs = spss.Cursor([12])
        values = curs.fetchall()
        print values
        curs.close()
        if (None,) in values:
          print 'ok1'

           break
         
        else:
         print 'ok'
         spss.Submit("INSERT FILE='c:/temp/block.sps'.")
    end program.

    in the output :

    ((12.85061,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,))

    ok1

    it is strange, ok didn't appear, although 12.85061 isn't a None

    block.sps didn't be run too.

     

     

     

     

     

     

  • JonPeck
    JonPeck
    269 Posts

    Re: conditional aggregation

    ‏2013-07-04T12:29:45Z  

    Hello Jon

    i let the following lines run:

    get file = "C:\temp\test1.sav".


    begin program.
    import spss
    while True:
        curs = spss.Cursor([12])
        values = curs.fetchall()
        print values
        curs.close()
        if (None,) in values:
          print 'ok1'

           break
         
        else:
         print 'ok'
         spss.Submit("INSERT FILE='c:/temp/block.sps'.")
    end program.

    in the output :

    ((12.85061,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,))

    ok1

    it is strange, ok didn't appear, although 12.85061 isn't a None

    block.sps didn't be run too.

     

     

     

     

     

     

    Since (None,) does occur, apparently, initially, that code prints ok1 and exits the loop, so the else clause is never executed.

  • redrose888
    redrose888
    100 Posts

    Re: conditional aggregation

    ‏2013-07-04T12:54:36Z  
    • JonPeck
    • ‏2013-07-04T12:29:45Z

    Since (None,) does occur, apparently, initially, that code prints ok1 and exits the loop, so the else clause is never executed.

    that means there is no solution for my problem :-(

    I could do it manually, I think there must be a mathod doing it automatically

  • redrose888
    redrose888
    100 Posts

    Re: conditional aggregation

    ‏2013-07-04T13:58:50Z  
    • JonPeck
    • ‏2013-07-04T12:29:45Z

    Since (None,) does occur, apparently, initially, that code prints ok1 and exits the loop, so the else clause is never executed.

    Hello Jon,

    I solved this problem, thank you very much! without your suggestion with cursor I wouldn't have fix it

     

    get file = "C:\temp\test.sav".

    compute t = 0.

    AGGREGATE
      /OUTFILE=* MODE = ADDVARIABLES OVERWRITE = YES
      /BREAK=t
      /myvar_sum =SUM(myvar).

    begin program.
    import spss
    while True:
        curs = spss.Cursor([22])
        values = curs.fetchall()
        curs.close()
        if (None,) in values:
           print 'geschafft'
           break      
        spss.Submit("INSERT FILE='c:/temp/block.sps'.")
    end program.

    regards,