SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 


#Analytics
#SPSSStatistics
#Analyticstools
 View Only
  • 1.  SpssOutputDoc.SetAsDesignatedOutputDoc() won't work reliably when INSERTed

    Posted 09/13/22 04:49 PM
    I need to direct the output of Python's print() to various output documents. The print statements exist in a series of separate syntax files. These files are called one after the other with INSERT FILE = <path>.

    Currently I am using custom python functions to switch the designated output doc. This is not working under all circumstances. Please see the comments in the syntax below.

    In layman's terms, the Python code appears to "overtake itself" during execution.

    How can I ensure that the prints are routed correctly?
    The real use case relates not only to print(), but also to the output of regression analyses, etc.

    * Encoding: UTF-8.
    BEGIN PROGRAM PYTHON3.
    import spss, SpssClient

    SpssClient.StartClient()
    out_a = SpssClient.NewOutputDoc()
    spss.Submit ('OUTPUT NAME A')
    out_b = SpssClient.NewOutputDoc()
    SpssClient.StopClient()
    spss.Submit ('OUTPUT NAME B')

    def print_a (msg):
    # prints to out_a
    SpssClient.StartClient()
    out_a.SetAsDesignatedOutputDoc()
    SpssClient.StopClient
    print(msg)

    def print_b (msg):
    # prints to out_b
    SpssClient.StartClient()
    out_b.SetAsDesignatedOutputDoc()
    SpssClient.StopClient
    print(msg)
    END PROGRAM.


    * ------------------------------------.
    * This prints everything to out_b :( .
    BEGIN PROGRAM PYTHON3.
    print_a ('A 1')
    print_b ('B 1')
    print_a ('A 2')
    print_b ('B 2')
    END PROGRAM.


    * ------------------------------------.
    * This switches between outputs as expected :) .
    BEGIN PROGRAM PYTHON3.
    print_a ('A 3')
    END PROGRAM.

    BEGIN PROGRAM PYTHON3.
    print_b ('B 3')
    END PROGRAM.

    BEGIN PROGRAM PYTHON3.
    print_a ('A 4')
    END PROGRAM.

    BEGIN PROGRAM PYTHON3.
    print_b ('B 4')
    END PROGRAM.

    * ------------------------------------.
    * This, however, keeps printing to (the previously designated) out_b :( .
    INSERT FILE = '/Path/to/inserted.sps'.

    * Given inserted.sps contains

    * BEGIN PROGRAM PYTHON3.
    * print_a ('A 5')
    END PROGRAM.

    * BEGIN PROGRAM PYTHON3.
    * print_b ('B 5')
    END PROGRAM.

    * BEGIN PROGRAM PYTHON3.
    * print_a ('A 6')
    END PROGRAM.

    * BEGIN PROGRAM PYTHON3.
    * print_b ('B 6')
    END PROGRAM.







    ------------------------------
    Frank Watzl
    ------------------------------

    #SPSSStatistics


  • 2.  RE: SpssOutputDoc.SetAsDesignatedOutputDoc() won't work reliably when INSERTed

    Posted 09/13/22 05:21 PM
    Output document names don't work like datasets.  They don't need names in order to survive.
    Typically, you would create a new one, which would automatically become the active output doc, I think, and then use that object for control,.

    For example, to switch to another one, you would just use 
    SetAsDesignatedOutputDoc() on that item.

    The output document list gives you a list of all of them, but there is no name property for them.  If you need to keep track of them, you could use Get/SetHeaderText on the item as an identifier.

    If you do need to reference these by names assigned by OUTPUT ACTIVATE etc, you might need to put in a sleep call for a second or 2 to give it time to synchronize.

    Caveat:  I haven't worked much with programmatically controlled output documents.

    --