SPSS Statistics

SPSS Statistics

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

 View Only

SPSS macro problem with substr and condition

  • 1.  SPSS macro problem with substr and condition

    Posted Fri October 18, 2024 02:01 PM

    Hi All, 

    I have written a macro that uses a list of variables, ending in the letters of the alphabet (A, B, C etc.).

    It removes the last letter of the variable name and concatenates the remaining substring with _1 (if the last letter was A), _2 (if the last letter was "B") etc and renames the original variable to the new concatenated string. 

    However, I need a condition: if the last letter of the variable name is "A", I have to start the count again.

    All in all this is what I try to do:

    trackA-->track_1

    ...

    trackI-->track_9

    trace1-->trace_1

    ...

    traceD-->trace_4

    And this is my solution that does not work:

    define !rename4 (vlist = !charend('/'))
    !let !count = 1
    !let !incr = 1
    !let !subtr=-1
    !do !vname !in (!vlist)   
        !let !len=!length(!vname)
        !let !lastbutonecharacter = !length(!concat(!blank(!len), !blank(!subtr))) 
        !let !lastletter= !last(!vname) 
        !if (!lastletter = 'A') !then 
        !let !count=1  
        !ifend
        !let !newname0 = !substr(!vname, 1, !lastbutonecharacter) 
        !let !newname1 = !concat(!newname0,"_")
        !let !newname = !concat(!newname1, !count)
        rename variables (!vname = !newname).
        !let !count = !length(!concat(!blank(!count), !blank(!incr)))
    !doend
    !enddefine.

    If the condition is removed, it works correctly. 

    Could you please help me correcting my code?

    Thanks in advance!



    ------------------------------
    Otília Csörgő
    ------------------------------