Original Message:
Sent: Mon October 21, 2024 02:00 AM
From: Otília Csörgő
Subject: SPSS macro problem with substr and condition
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ő
Original Message:
Sent: Sun October 20, 2024 12:02 PM
From: Kirill Orlov
Subject: SPSS macro problem with substr and condition
Are you sure !last() function exists in SPSS macro language?
------------------------------
Kirill Orlov
Original Message:
Sent: Fri October 18, 2024 05:46 AM
From: Otília Csörgő
Subject: SPSS macro problem with substr and condition
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ő
------------------------------