SPSS Statistics

 View Only
  • 1.  creating variable based off of file name

    Posted Mon October 25, 2021 09:45 AM
    Morning, I'm using several files over a period of time.  They have a standard naming configuration such as 'Active January 2017', Active February 2017' etc..  Is there a way to create a variable and include all or part of the file name in the variable?  thanks.

    ------------------------------
    Art Jack
    ------------------------------

    #SPSSStatistics


  • 2.  RE: creating variable based off of file name

    Posted Mon October 25, 2021 10:38 AM
    Good morning, Art. A couple of questions: Just the file name and not the path? Replace spaces in name with underlines OK?

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 3.  RE: creating variable based off of file name

    IBM Champion
    Posted Mon October 25, 2021 12:02 PM
    I'm not clear on whether you want the variable name to be constructed from the datafile name or you want a variable whose value is the filename.  Here are two versions of a short program for these two possibilities.  For the second version, if the entire path might exceed 64 bytes, it could be modified to remove the directory portion of the string.

    * version 1.
    * compute a variable named file holding the name of the active datafile.
    begin program python3.
    import spss, spssaux

    # if the active file does not have a name, the generated variable will be blank    
    filename = spssaux.GetDatasetInfo()
    spss.Submit("""string file(a64).
    compute file="%s".""" % filename)
    end program.

    * version 2.
    * compute a variable whose name is constructed from the name of the active datafile.
    * Only a cursory check is made for characters not legal in filenames.
    * path must be no more than 64 bytes long.
    begin program python3.
    import spss, spssaux, re
        
    filename = spssaux.GetDatasetInfo()
    filename = re.sub(r"[ /,+-=*()\\]", "_", filename)
    spss.Submit("""string %(filename)s(a64).
    compute %(filename)s ="%(filename)s".""" % locals())
    end program. 
    --





  • 4.  RE: creating variable based off of file name

    Posted Mon October 25, 2021 01:45 PM
    Edited by System Fri January 20, 2023 04:23 PM
    If all you wanted was the new variable name to contain the filename, the new variable could be any format, not just STRING. Just change instances of the STRING command to the NUMERIC command.

    For example:

    begin program python3.
    import os, spss
    original="/Users/rick/DATA/languages/English/Employee data.sav"
    fileinfo=os.path.split(original)
    mypath=fileinfo[0]
    myfile=fileinfo[1]
    clean_filename=myfile.replace(' ','_')
    spss.Submit("GET FILE '" + original + "'.")
    spss.Submit("NUMERIC " + clean_filename + " (F8.2).")
    spss.Submit("EXECUTE.")
    end program.​


    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------