SPSS Statistics

 View Only
  • 1.  Using most recent file with common name structure

    Posted Mon December 13, 2021 03:24 PM
    Afternoon, I'm trying to write something that I can put into SPSS production.  I need to pull in the most recent version of whatever file is available, without having to go in and actually change the name.  For the active file if there is a more recent version I need to use that one. Thanks in advance. 

    dataset close all.
    get file='e:\data\2022\active 11 29 2021.sav'.

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


  • 2.  RE: Using most recent file with common name structure

    Posted Mon December 13, 2021 03:34 PM
    This is something you might be able to do with Python. See this, for example.

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



  • 3.  RE: Using most recent file with common name structure

    Posted Mon December 13, 2021 03:35 PM
    If you have some control over the file naming, you might consider using the CreateFileNameWDate and FindMostRecentFile functions in the spssaux2.py module shipped with Statistics.  These expect a sortable date string, which the format you show does not without some care in the naming.

    The doc for the second function says,
    Return the full filename, including path if included in basename, 
        of the most recent file matching basename or None if there is no match.
        
        Matching means the filename equals the basename with or without a timestamp.
        A timestamp has the format
        _YYYY-MM-DD_HH-MM 
        just before the extension.  

    which could then be opened with code in the BEGIN PROGRAM block, or that code could create a macro to be used outside that program.

    If you can't control the date string, more elaborate code would be required to parse the date and then select from the qualifying files.

    --





  • 4.  RE: Using most recent file with common name structure

    Posted Wed December 29, 2021 07:54 AM
    Morning, I came up with this.  Thanks for your help.

    dataset close all.
    set unicode=YES.
    NEW FILE.
    DATASET NAME DataSet1 WINDOW=FRONT.
    BEGIN PROGRAM python3.
    import os
    import shutil
    import glob
    import spss
    mypath = "C:/datab/2022/*Active*"
    ActivePath=(max(glob.glob(mypath), key=os.path.getmtime))
    print(ActivePath)
    target =r'C:\datab\2022\current\active_current.sav'
    shutil.copyfile(ActivePath,target)
    end program.
    set unicode=off.

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



  • 5.  RE: Using most recent file with common name structure

    Posted Wed December 29, 2021 09:16 AM
    Clever.  I don't think I have ever used the key argument with max, but this is a great usage.

    If you want to avoid copying the file, you could use the spss.SetMacroValue api to define a macro with the filespec and using that with GET.

    --