SPSS Statistics

 View Only
  • 1.  Automating file name

    Posted Wed January 24, 2024 12:59 PM

    Hello,
    I have a SPSS program which reads a file named as "filename01012024.txt" as i am showing below:


    GET DATA
       /TYPE=txt
      /FILE='C:\.....\filename01012024.txt'
      .........................

    EXECUTE.
    DATASET NAME filename01012024 WINDOW=FRONT.

    Because i need to run this program each time new text file is generated , i am thinking to create a macro, with date as parameter ,

    and to use it in above code, so I do not need to change manually the date extension, part of the file name.

    How this macro can be created and used in this scenario?

    Thank you.

     



    ------------------------------
    IBMcode SPSS
    ------------------------------


  • 2.  RE: Automating file name

    Posted Thu January 25, 2024 05:15 AM
    Edited by Patrick Henry-Lucet Thu January 25, 2024 05:15 AM

    Hello there.

    I use something similar when working on different waves for a survey (with Excel files).

     
    define !date()
     [your date here]
    !enddefine.

    define !qdate()
     !quote(!eval(!date))

    !enddefine.


    get data /type = xlsx
    /file = 'filename' + !qbase + '.xlsx'
      /sheet = name '_main_'
      /cellrange = full
      /readnames = on
      /assumedstrwidth = 32767.
    dataset name filename!date
       window = front.



    ------------------------------
    Patrick Henry-Lucet
    ------------------------------



  • 3.  RE: Automating file name

    IBM Champion
    Posted Thu January 25, 2024 09:53 AM
    You can use a macro for this sort of thing, but a simpler solution for file specs is just to define a FILE HANDLE at the top of the job.  For example,
    FILE HANDLE DATA /NAME="C:/mydata/01/02/03.xls".
    and just refer to it in the GET DATA command
    GET DATA /FILE=DATA ...

    If you want to show the file used in your output, use a 
    SHOW HANDLES.
    command.

    --





  • 4.  RE: Automating file name

    Posted Thu January 25, 2024 10:29 AM

    Hi Jon,

    I agree with your suggestion.

    However, the question i asked for the macro helps my understanding of SPSS macros.

    Thank you .



    ------------------------------
    IBMcode SPSS
    ------------------------------



  • 5.  RE: Automating file name

    IBM Champion
    Posted Thu January 25, 2024 10:34 AM
    My opinion is that macros are best used only for simple situations such as a static  definition, although I have seen macros that are thousands of lines long that work.  Rather than macros, I much prefer Python or R programmability, which is much more powerful.

    The macro doc in the CSR is less than ideal for learning.  If you do want to explore it further, I suggest this source

    --





  • 6.  RE: Automating file name

    Posted Thu January 25, 2024 10:23 AM
    Edited by IBMcode SPSS Thu January 25, 2024 10:25 AM

    Hello Patrick, 

    Thank you for reply.

    I wanted to ask, before running the get data statement, i would like to check what are the values assigned to these macros created above? 

    For instance, in SAS I would have done:
    %put &date; and in the SAS log would have been printed its value .

    Thank you.



    ------------------------------
    IBMcode SPSS
    ------------------------------



  • 7.  RE: Automating file name

    IBM Champion
    Posted Thu January 25, 2024 10:29 AM
    There is no direct way to show macro definitions.  DISPLAY MACROS will show the names of defined macros but not the definitions.

    However, if you run SET MPRINT ON, syntax after macro expansion will be displayed in the log.  This is another reason why FILE HANDLE is a better solution.

    --





  • 8.  RE: Automating file name

    Posted Thu January 25, 2024 11:22 AM

    Ok,

    I understand now.

    Thank you for time.



    ------------------------------
    IBMcode SPSS
    ------------------------------