0

I want to rename a list of variables in a dataset that has thousands of variables. There are certain patterns in the renaming process and I tried to write a macro for this.

I want to drop the last character of the variable's name and concatenate it with new characters(this part works fine)

Here is the macro. it does not work but the output file is of no use this time:

define !rename4 (vlist = !charend('/'))
!let !count = 1
!let !incr = 1
!do !vname !in (!vlist)  
!let !lastcharacter = !char.length(!vname)
!let !newname0 = !char.substr(!vname, 1, !lastcharacter) \*\*
!let !newname1 = !concat(!newname0,"\_")
!let !newname = !concat(!newname1, !count)
rename variables (!vname = !newname)
!let !count = !length(!concat(!blank(!count), !blank(!incr)))
!doend
!enddefine.
1
  • This looks like something I could help with (after the weekend) but you need to clarify - what happens exactly? I couldn't understand what works and what doesn't, and what's that about the output file? best if you can give an example of a small dataset and the expected results, so we can try our suggestions on. Commented Oct 18, 2024 at 13:59

1 Answer 1

0

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 !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.

the main problem was that I had no idea about what commands I can use in an SPSS macro. In the IBM community Jon Oeck helped me out: it can be found in SPSS help menu in the command syntax reference (under define-enddefine).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.