SPSS Statistics

 View Only
Expand all | Collapse all

How to get access to a dataset from Visual Studio Code or IDLE?

  • 1.  How to get access to a dataset from Visual Studio Code or IDLE?

    Posted Mon May 16, 2022 08:34 AM
    I'm trying to write prioprietary, local extension command in Python 3. I successfully connected Visual Studio Code to SPSS. However, when I try to get variable list of the active dataset I always receive nothing.
    import spss

    spss.StartDataStep()
    print(spss.ActiveDataset()) # returns "*" in VSC
    print(spss.GetCaseCount()) # returns 0 in VSC
    spss.EndDataStep()
    ​When I tried to run the same code in Python 3 IDLE opened from SPSS - still the same. However, when I run the code above from `begin program`-`end program` block - everything's fine.

    Is there any way to write code in VSC or at least IDLE? I mean in terms of writing extensions​ syntax editor is not convenient.

    Note: when I use `SpssClient` I can get, for example, active dataset name. Nonetheless, as far as I know, there is no way to access other, useful dataset properties (like variables list) using this class. Is it in fact possible and I'm simply missing something?

    ------------------------------
    Konrad Gałuszko
    ------------------------------

    #SPSSStatistics


  • 2.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    IBM Champion
    Posted Mon May 16, 2022 08:53 AM
    I don't use VS Code with Statistics, but you should be able to do this as long as the Python path points to the location where Statistics is installed.  I use Wing IDE frequently in external mode with the Python executable set to the location where the SPSS Python installation is.

    However, you seem to be able to import the spss module, so other thing should work.  You would need, however, to use the spss.Submit api to open or create a dataset before those other apis would work.  In external mode, Python does not connect to a running instance of SPSS.  It would start a new instance.

    ------------------------------
    Jon Peck
    ------------------------------



  • 3.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    Posted Mon May 16, 2022 08:59 AM
    Hi Jon, thanks for answer.

    Sadly, I don't understand you. I have an opened dataset and, at the same time, would like to edit code in VSC or IDLE (Python 3 from SPSS folder). How to connect to this already existing instance?

    ------------------------------
    Konrad Gałuszko
    ------------------------------



  • 4.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    IBM Champion
    Posted Mon May 16, 2022 09:09 AM
    You don't have an opened dataset in the external-mode instance of Statistics until you run commands to populate that instance with data.  You could do something like this in your external code.

    import spss
    spss.Submit(r"""get file=c:/spss28/samples/english/employee data.sav".""")

    replacing the filespec with one appropriate to your installation.
    The spssaux module, btw, has a convenience function to do this
    import spssaux
    spssaux.OpenDataFile(r"c:/spss28/samples/english/employee data.sav")

    --





  • 5.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    Posted Mon May 16, 2022 09:23 AM
    Okay, now I understand. Still, I'm planning to install this script as my own extension command. So, opening the active dataset again may cause damage to the file. Last but not least - how to furhter run this syntax to see changes in the active dataset in an already opened window of SPSS?

    ------------------------------
    Konrad Gałuszko
    ------------------------------



  • 6.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    IBM Champion
    Posted Mon May 16, 2022 09:33 AM
    If you want to use this with an already active SPSS session, why not insert it or import it via a BEGIN PROGRAM block?  You could use external mode to develop the code but then insert it in the active session.  Typically, you would write your code as a function and then have code like this at the end

    if __name__ == "main":
       myfunction()
    which would call that function defined in that file if this is run externally but not if internal.

    However, the SpssClient api will connect to an existing session like this.
    SpssClient.StartClient()
    data=SpssClient.GetActiveDataDoc()
    data.GetCaseCount()
    474

    --





  • 7.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    Posted Mon May 16, 2022 09:35 AM
    SpssClient does what I need but how, for example, get variables list? I don't see any way

    ------------------------------
    Konrad Gałuszko
    ------------------------------



  • 8.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    IBM Champion
    Posted Mon May 16, 2022 10:22 AM
    The SpssClient. api provides very limited access to the variable dictionary.  
    The GetActiveDataDoc api only gives you access to the properties of the window and a few other things.  You would need to use the spss or spssaux or spssdata modules to work with the contents.  So, again, I suggest developing your code in external mode without connecting to an existing session but then running the code in internal mode.

    --





  • 9.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    Posted Mon May 16, 2022 04:54 PM
    Thanks Jon, now I get it. SpssClient is designed for active, opened sessions without the possibility to read dataset (thus, for example, for output window) while spss/spssaux/spssaux2 is used to read a dataset from a new instance (external mode) OR from an opened dataset (internal mode: begin program.-end program.).
    By the way, how custom extension commands deal with ranges of variables? For instance, if I mention variables like


    SOME CUSTOM COMMAND
    VARIABLES = v1 to v3.

    SOME CUSTOM COMMAND
    VARIABLES = all.

    what list of variables will be passed to the Python command body?



    ------------------------------
    Konrad Gałuszko
    ------------------------------



  • 10.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    IBM Champion
    Posted Mon May 16, 2022 05:18 PM
    If you use the extension.py module to process the command, which I would recommend, ALL will be automatically expanded into a variable list.  If not, it is up to you to expand the list, which will be received just as all.

    You can alternatively expand all using the spssaux.VariableDict class, which has an expand method.  If that class is used without specifying a variable list, it will return a variable dictionary object with all the variables.

    --





  • 11.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    Posted Mon May 16, 2022 05:31 PM
    Okay, but what with TO keyword? Does it work the same way as ALL?

    ------------------------------
    Konrad Gałuszko
    ------------------------------



  • 12.  RE: How to get access to a dataset from Visual Studio Code or IDLE?

    IBM Champion
    Posted Mon May 16, 2022 05:34 PM
    Yes.  Expand also works with TO.  It even has a small extra feature.  If you just write X TO
    that is interpreted as to the end of the variable list.
    --