SPSS Statistics

SPSS Statistics

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

 View Only
  • 1.  Spss macro

    Posted 23 days ago

    Im trying to come up with a totally spss solution for file splitting & came across this on SPSS tools .net. It does work but there are some long spaces after the text within the file names after the split. I'm guessing there would be a way to use !length or !substr to get rid of them? I've had a hard time tracking down SPSS macro info. thanks.

    file handle ET /name='c:\et'.

     

    dataset close all.
    get file='C:\DataB\2025\School 12 10 2024.sav' /keep stno sch.name meno /rename sch.name=school.
    select if not missing(stno).
    sort cases by stno.
    if stno=lag(stno) drop=1.
    select if missing(drop).
    save outfile='ET\sch_temp2.sav' /drop drop.
    execute.

     

    * Now start the job.
    set mprint=on.

     

    *//////////////////////////////////////////.
    DEFINE !SPLIT (var=!TOKENS(1) /value=!TOKENS(1) )
    TEMPORARY.
    SELECT IF (!var=!value).
    SAVE TRANSLATE OUTFILE=!QUOTE(!CONCAT('C:\DataB\e25\test\macro_split\',!UNQUOTE(!value),'.csv'))
      /drop=key
      /TYPE=CSV
      /ENCODING='Locale'
      /MAP
      /REPLACE
      /FIELDNAMES
      /CELLS=VALUES.
    execute.
    !ENDDEFINE.
    *//////////////////////////////////////////.

     

    * Find all different existing values of cat1.
    AGGREGATE  /OUTFILE=*
      /BREAK=key  /notused = N.

     

    * Write a syntax file which will call the above macro.
    WRITE OUTFILE='C:\DataB\e25\test\macro_split\temp.sps'
    /'!SPLIT var=key value="'key'".'.
    Execute.

     

    * Get the original data file and do the macro calls.
    GET FILE='C:\DataB\e25\test\macro_split\mydata.sav'.
    insert file = 'C:\DataB\e25\test\macro_split\temp.sps'.



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


  • 2.  RE: Spss macro

    Posted 22 days ago

    Art, you probably referring to syntax https://www.spsstools.net/en/syntax/syntax-index/working-with-many-files/split-big-files-into-separate-categories-string-var/ on spsstools.

    For me, it worked smoothly. Try the below syntax which I tied, line by line to track what's different with yours in its behaviour.

    dataset close all.
    DATA LIST FREE /key(A2).
    BEGIN DATA
    "A2" "B7" "01" "08" "W7" "XY"
    "A2" "A2" "08" "W7" "W7" "XY"
    "A2" "B7" "01" "08" "W7" "XY"
    END DATA.
    LIST.
    dataset name mydata.

    DEFINE !SPLIT (var=!TOKENS(1) /value=!TOKENS(1) )
    TEMPORARY.
    SELECT IF (!var=!value).
    SAVE TRANSLATE OUTFILE=!QUOTE(!CONCAT('C:\temp\',!UNQUOTE(!value),'.csv'))
      /TYPE=CSV
      /ENCODING='Locale'
      /MAP
      /REPLACE
      /FIELDNAMES
      /CELLS=VALUES.
    execute.
    !ENDDEFINE.
    *//////////////////////////////////////////.

    * Find all different existing values of cat1.
    AGGREGATE  /OUTFILE=*
      /BREAK=key  /notused = N.

    * Write a syntax file which will call the above macro.
    WRITE OUTFILE='C:\temp\temp.sps'
     /'!SPLIT var=key value="'key'".'.
    Execute.

    * Get the original data file and do the macro calls.
    dataset activate mydata.
    insert file = 'C:\temp\temp.sps'.



    ------------------------------
    Kirill Orlov
    ------------------------------



  • 3.  RE: Spss macro

    Posted 22 days ago

    Hi Kirill,

    Yes Raynald's SPSS tools site.  I think the biggest difference in the code we have is the variables used are different string lengths.  So in your instance they're all a2.  Mine have actual school name's for the key to break on and rename as. The file name's are showing up like this after splitting "School A             .csv" "School B  .csv" "School C                 .csv".  I would think something like this might work? SAVE TRANSLATE OUTFILE=!QUOTE(!CONCAT('C:\\DataB\\e25\\test\\macro_split\\', !SUBSTR(!value,1,!LENGTH(!value)), '.csv')) /TYPE=CSV.  



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



  • 4.  RE: Spss macro

    Posted 22 days ago

    Try !EVAL(!UNQUOTE(!value)) rather than !UNQUOTE(!value) in the concat line.



    ------------------------------
    Kirill Orlov
    ------------------------------



  • 5.  RE: Spss macro

    Posted 21 days ago

    This works thanks Kirill. - Art Jack



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