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!
Answer by redrose888 (0) | Jul 03, 2013 at 06:30 AM
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
Answer by JonPeck (4671) | Jul 03, 2013 at 12:50 PM
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.
Answer by redrose888 (0) | Jul 03, 2013 at 12:58 PM
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.
Answer by JonPeck (4671) | Jul 03, 2013 at 01:17 PM
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
Answer by redrose888 (0) | Jul 03, 2013 at 01:41 PM
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?
Answer by redrose888 (0) | Jul 03, 2013 at 02:28 PM
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,) ?
Answer by redrose888 (0) | Jul 04, 2013 at 10:03 AM
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.