Discussion:
Remove suffix from variable names
(too old to reply)
winterversion
2021-08-20 19:25:41 UTC
Permalink
Hello,

I have a suffix, "_t1" that I would like to remove from all of my variables. Is there a handy set of syntax to do this for all of my variables at once rather than having rename them all one by one?

Thanks in advance!
Paul
Rich Ulrich
2021-08-21 05:37:46 UTC
Permalink
On Fri, 20 Aug 2021 12:25:41 -0700 (PDT), winterversion
Post by winterversion
Hello,
I have a suffix, "_t1" that I would like to remove from all of my variables. Is there a handy set of syntax to do this for all of my variables at once rather than having rename them all one by one?
Thanks in advance!
There's probably a way using Python, if you use Python in SPSS.

I've used the brute force method. Copy the variable list twice and
shorten the second set of names. Finish the syntax as appropriate.

The syntax guide gives the RENAME example, for two variables -
RENAME VARIABLES (MOHIRED YRHIRED=MOSTART YRSTART).


That was convenient since I did have a simple list of variables
and I regularly used an editor with a useful SUBSTITUTE command.
Substitute (for your case) "_t1" to blank for a given set of lines.
I don't know what the SPSS syntax editor can do.
--
Rich Ulrich
winterversion
2021-08-23 20:01:21 UTC
Permalink
Thank you Rich! I actually discovered that you can do "find/replace" under the variable view tab. When you highlight the column with all the variable names and press control F, you can do a replace "_t1" with a blank, and that worked.
Rich Ulrich
2021-08-24 15:35:22 UTC
Permalink
On Mon, 23 Aug 2021 13:01:21 -0700 (PDT), winterversion
Post by winterversion
Thank you Rich! I actually discovered that you can do "find/replace" under the variable view tab. When you highlight the column with all the variable names and press control F, you can do a replace "_t1" with a blank, and that worked.
Great! You improved on the solution! (at least, for the
one-time requirement).

I'm thinking, there is a potential advantage of the solution
done by tedious syntax -- In my experience of analyzing data
from other people, it is not uncommon for them to want to
edit some values in the set, or to add a few cases... or even
do the whole thing with some other data. Having syntax
(a) documents the process and (b) allows for instant re-runs.

Leaving everything useful in (saved) syntax is a good idea in
the long run, even if you have interactive experiments that you
throw away.
--
Rich Ulrich
Bruce Weaver
2021-08-28 15:54:02 UTC
Permalink
Here's some Python code that seems relevant:

https://www.py4u.net/discuss/209789

The UCLA link it includes is out of date. I suspect that this is the current version of the page that was intended:

https://stats.idre.ucla.edu/spss/faq/how-can-i-write-a-python-program-to-rename-variables-in-spss/

HTH.
Post by Rich Ulrich
On Mon, 23 Aug 2021 13:01:21 -0700 (PDT), winterversion
Thank you Rich! I actually discovered that you can do "find/replace" under the variable view tab. When you highlight the column with all the variable names and press control F, you can do a replace "_t1" with a blank, and that worked.
Great! You improved on the solution! (at least, for the
one-time requirement).
I'm thinking, there is a potential advantage of the solution
done by tedious syntax -- In my experience of analyzing data
from other people, it is not uncommon for them to want to
edit some values in the set, or to add a few cases... or even
do the whole thing with some other data. Having syntax
(a) documents the process and (b) allows for instant re-runs.
Leaving everything useful in (saved) syntax is a good idea in
the long run, even if you have interactive experiments that you
throw away.
--
Rich Ulrich
Loading...