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 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. I need to rename them: remove the last letter and concatenate the remaining string with an underscore + number (respective to the last letter's position in the alphabet).

    I also need a condition: if the last letter was A, I have to start the count from number 1 again.

    Without the condition the macro works fine. 

    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.

    What can be the problem? Could you please help me correct it?

    Thanks in advance.



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


  • 2.  RE: SPSS macro problem with substr and condition

    Posted Sun October 20, 2024 12:02 PM

    Are you sure !last() function exists in SPSS macro language?



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



  • 3.  RE: SPSS macro problem with substr and condition

    Posted Mon October 21, 2024 02:00 AM

    Hi, 

    No, I was not sure at all :) 

    However, I managed to solve the problem the following way:

    define !rename4 (vlist = !charend('/'))
    !let !count = 1
    !let !incr = 1
    !let !incrtwo=2
    !let !subtr=-1
    !do !vname !in (!vlist)   
        !let !len=!length(!vname)
        !let !lastbutonecharacter = !length(!concat(!blank(!len), !blank(!subtr))) 
        !let !lastcharacter=!length(!concat(!blank(!lastbutonecharacter), !blank(!incrtwo)))
        !let !lastletter= !substr(!vname, !len, !incr)
        !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.

    It may not be the cleanest solution, but it works and for the time being this is the most important for me. 

    By the way: is there a source where I can check the functions that can be used in SPSS macros?

    Thanks for the answer in advance,

    Oti



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



  • 4.  RE: SPSS macro problem with substr and condition

    Posted Tue October 22, 2024 08:36 AM

    See the list of functions here:

    • https://www.ibm.com/docs/en/spss-statistics/29.0.0?topic=enddefine-string-manipulation-functions-define-command



    ------------------------------
    Bruce Weaver
    ------------------------------



  • 5.  RE: SPSS macro problem with substr and condition

    Posted Wed October 23, 2024 01:50 PM
    The Command Syntax Reference manual contains a complete description of the macro facility under the DEFINE command.  The CSR is installed on the SPSS Help menu.

    --





  • 6.  RE: SPSS macro problem with substr and condition

    Posted Thu October 24, 2024 02:11 AM

    Thank you! I had no idea that I can find this list within SPSS help menu. I tried looking for this information on the internet, however, I could not find one source with all the commands collected together (obviously the reason for this is that there is no need for such a list as it is included in SPSS). Another reason for my unsuccessful search was that I googled SPSS macro, instead of define enddefine...

    Thank you again for your time and help!



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