OMS in Python loop

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

OMS in Python loop

Just In
Hi listers,

My task is to create Crosstabs output tables for many distinct groups
from the same two variables. My strategy is output a table for each
group and stack them up. Please see the testing code.

My questions are: Why am I getting the error below?

end program.
Traceback (most recent call last):
   File "<string>", line 30, in <module>
TypeError: not enough arguments for format string
set mprint off.

Also, is there a better strategy to tackle this problem?

Thank you!


GET FILE = 'c:\all.sav'.
set mprint on.
begin program.
import spss
alist = ['G001', 'G002', 'G004']
adim = len(alist)
for i in range(adim):
    grp = alist[i]
    spss.Submit(r"""
            FILTER OFF.
            USE ALL.
            COMPUTE fil =(Nn = "I1-1" and AID = '%s').
            FILTER BY fil.
            EXECUTE.

            OMS
              /SELECT TABLES
              /IF COMMANDS=['Crosstabs'] SUBTYPES=['Chi Square Tests' '
Directional Measures' ' Warnings']
              /DESTINATION FORMAT=SAV NUMBERED=TableNumber_
              OUTFILE="c:\\temp\'%s'.sav"
              /COLUMNS SEQUENCE=[RALL CALL LALL].

            CROSSTABS
              /TABLES=Scale BY GroupScore
              /FORMAT=AVALUE TABLES
              /STATISTICS=CHISQ D
              /CELLS=COUNT
              /COUNT ROUND CELL.

            OMSEND .

             """ %(grp))
end program.
set mprint off.

=====================
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: OMS in Python loop

Jon Peck
I'm not sure that I understand your data structure, but if the groups are actually distinct, perhaps you could just compute a group variable and use SPLIT FILES to get all the results from one CROSSTABS command.

However, the problem with your Python code is that you have two substitution parameters (%s) but are supplying only one value.  You need to provide two values even though they are the same, e.g.,
"""....""" % (grp, grp)

For big blocks of syntax, though, it is better to use named parameters and just pass locals() for the values.  E.g.
r"""... OUTFILE="c:\\temp\'%(grp)s'.sav" ...""" % locals()

Note, also, that for a single sav-format destination for OMS, all the table must be conformable, i.e., have the same number of columns.  And get rid of the EXECUTE commands.


On Wed, May 6, 2020 at 1:27 PM Just In <[hidden email]> wrote:
Hi listers,

My task is to create Crosstabs output tables for many distinct groups
from the same two variables. My strategy is output a table for each
group and stack them up. Please see the testing code.

My questions are: Why am I getting the error below?

end program.
Traceback (most recent call last):
   File "<string>", line 30, in <module>
TypeError: not enough arguments for format string
set mprint off.

Also, is there a better strategy to tackle this problem?

Thank you!


GET FILE = 'c:\all.sav'.
set mprint on.
begin program.
import spss
alist = ['G001', 'G002', 'G004']
adim = len(alist)
for i in range(adim):
    grp = alist[i]
    spss.Submit(r"""
            FILTER OFF.
            USE ALL.
            COMPUTE fil =(Nn = "I1-1" and AID = '%s').
            FILTER BY fil.
            EXECUTE.

            OMS
              /SELECT TABLES
              /IF COMMANDS=['Crosstabs'] SUBTYPES=['Chi Square Tests' '
Directional Measures' ' Warnings']
              /DESTINATION FORMAT=SAV NUMBERED=TableNumber_
              OUTFILE="c:\\temp\'%s'.sav"
              /COLUMNS SEQUENCE=[RALL CALL LALL].

            CROSSTABS
              /TABLES=Scale BY GroupScore
              /FORMAT=AVALUE TABLES
              /STATISTICS=CHISQ D
              /CELLS=COUNT
              /COUNT ROUND CELL.

            OMSEND .

             """ %(grp))
end program.
set mprint off.

=====================
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


--
Jon K Peck
[hidden email]

===================== 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: OMS in Python loop

Just In

Thank you, Jon! My problem was indeed the inconsistency of substitution parameters. Using SPLIT FILES, how to get the subgroup output stack up instead of concatenate as columns? Thanks again!

On 5/6/2020 2:41 PM, Jon Peck wrote:
I'm not sure that I understand your data structure, but if the groups are actually distinct, perhaps you could just compute a group variable and use SPLIT FILES to get all the results from one CROSSTABS command.

However, the problem with your Python code is that you have two substitution parameters (%s) but are supplying only one value.  You need to provide two values even though they are the same, e.g.,
"""....""" % (grp, grp)

For big blocks of syntax, though, it is better to use named parameters and just pass locals() for the values.  E.g.
r"""... OUTFILE="c:\\temp\'%(grp)s'.sav" ...""" % locals()

Note, also, that for a single sav-format destination for OMS, all the table must be conformable, i.e., have the same number of columns.  And get rid of the EXECUTE commands.


On Wed, May 6, 2020 at 1:27 PM Just In <[hidden email]> wrote:
Hi listers,

My task is to create Crosstabs output tables for many distinct groups
from the same two variables. My strategy is output a table for each
group and stack them up. Please see the testing code.

My questions are: Why am I getting the error below?

end program.
Traceback (most recent call last):
   File "<string>", line 30, in <module>
TypeError: not enough arguments for format string
set mprint off.

Also, is there a better strategy to tackle this problem?

Thank you!


GET FILE = 'c:\all.sav'.
set mprint on.
begin program.
import spss
alist = ['G001', 'G002', 'G004']
adim = len(alist)
for i in range(adim):
    grp = alist[i]
    spss.Submit(r"""
            FILTER OFF.
            USE ALL.
            COMPUTE fil =(Nn = "I1-1" and AID = '%s').
            FILTER BY fil.
            EXECUTE.

            OMS
              /SELECT TABLES
              /IF COMMANDS=['Crosstabs'] SUBTYPES=['Chi Square Tests' '
Directional Measures' ' Warnings']
              /DESTINATION FORMAT=SAV NUMBERED=TableNumber_
              OUTFILE="c:\\temp\'%s'.sav"
              /COLUMNS SEQUENCE=[RALL CALL LALL].

            CROSSTABS
              /TABLES=Scale BY GroupScore
              /FORMAT=AVALUE TABLES
              /STATISTICS=CHISQ D
              /CELLS=COUNT
              /COUNT ROUND CELL.

            OMSEND .

             """ %(grp))
end program.
set mprint off.

=====================
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


--
Jon K Peck
[hidden email]

===================== 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: OMS in Python loop

Jon Peck
Here is an example using the employee data.sav file shipped with Statistics.  It creates a dataset with all the splits tabulated

SORT CASES  BY jobcat.
SPLIT FILE LAYERED BY jobcat.

dataset declare xtabs.
oms select table /if subtypes='Crosstabulation'
/destination outfile=xtabs format=sav.
CROSSTABS
  /TABLES=educ BY gender
  /COUNT ASIS CELL.
omsend.

Unfortunately, OMS does not record the split value, but you can find the split boundaries in the output dataset by looking for Total.

On Wed, May 6, 2020 at 2:03 PM Just In <[hidden email]> wrote:

Thank you, Jon! My problem was indeed the inconsistency of substitution parameters. Using SPLIT FILES, how to get the subgroup output stack up instead of concatenate as columns? Thanks again!

On 5/6/2020 2:41 PM, Jon Peck wrote:
I'm not sure that I understand your data structure, but if the groups are actually distinct, perhaps you could just compute a group variable and use SPLIT FILES to get all the results from one CROSSTABS command.

However, the problem with your Python code is that you have two substitution parameters (%s) but are supplying only one value.  You need to provide two values even though they are the same, e.g.,
"""....""" % (grp, grp)

For big blocks of syntax, though, it is better to use named parameters and just pass locals() for the values.  E.g.
r"""... OUTFILE="c:\\temp\'%(grp)s'.sav" ...""" % locals()

Note, also, that for a single sav-format destination for OMS, all the table must be conformable, i.e., have the same number of columns.  And get rid of the EXECUTE commands.


On Wed, May 6, 2020 at 1:27 PM Just In <[hidden email]> wrote:
Hi listers,

My task is to create Crosstabs output tables for many distinct groups
from the same two variables. My strategy is output a table for each
group and stack them up. Please see the testing code.

My questions are: Why am I getting the error below?

end program.
Traceback (most recent call last):
   File "<string>", line 30, in <module>
TypeError: not enough arguments for format string
set mprint off.

Also, is there a better strategy to tackle this problem?

Thank you!


GET FILE = 'c:\all.sav'.
set mprint on.
begin program.
import spss
alist = ['G001', 'G002', 'G004']
adim = len(alist)
for i in range(adim):
    grp = alist[i]
    spss.Submit(r"""
            FILTER OFF.
            USE ALL.
            COMPUTE fil =(Nn = "I1-1" and AID = '%s').
            FILTER BY fil.
            EXECUTE.

            OMS
              /SELECT TABLES
              /IF COMMANDS=['Crosstabs'] SUBTYPES=['Chi Square Tests' '
Directional Measures' ' Warnings']
              /DESTINATION FORMAT=SAV NUMBERED=TableNumber_
              OUTFILE="c:\\temp\'%s'.sav"
              /COLUMNS SEQUENCE=[RALL CALL LALL].

            CROSSTABS
              /TABLES=Scale BY GroupScore
              /FORMAT=AVALUE TABLES
              /STATISTICS=CHISQ D
              /CELLS=COUNT
              /COUNT ROUND CELL.

            OMSEND .

             """ %(grp))
end program.
set mprint off.

=====================
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


--
Jon K Peck
[hidden email]

===================== 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


--
Jon K Peck
[hidden email]

===================== 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