Discussion:
Renaming varialbe names using loops
(too old to reply)
Vibeke Nielsen
2020-05-18 13:58:05 UTC
Permalink
Using 'restructure cases into variables' I have converted a SPSS file in long format (child/twin level) to wide format (family level). The variables that differ between the twin-members (child 1 is firstborn twin-member, child 2 is second-born twin-member) of one family ( e.g. handedness, IQ) now end at .1 for child 1 and .2 for child 2. For all those variables (string and numeric variables) I would like to rename them in order that .1 becomes _C1 and .2 becomes _C2. All variables names have different length but all end at '.1' or '.2'.

So for example, I have data concerning IQ and handedness of a twin pair: WISC_TIQ.1 and Handedness.1 which represents IQ and handedness of child 1, and WISC_TIQ.2 and Handedness.2 which represents IQ and handedness of child 2. Instead of renaming all those variables by hand, I wondered how to use loops to rename all these different variables ending at .1 into different variables all ending at _C1 and all variables ending at .2 into variables ending at _C2.

On https://stats.idre.ucla.edu/spss/code/a-few-spss-loops-for-renaming-variables-dynamically/, I read something about using a macro and loops for this, but as I am quite new to SPSS I fail to rewrite the examples on those sites to my issue.

Some suggest using the python extension, but unfortunately, the remote system of my university does not allow reïnstalling SPSS 25 to install the python plugin.

I hope someone can help me.
Bruce Weaver
2020-05-18 16:07:49 UTC
Permalink
Post by Vibeke Nielsen
Using 'restructure cases into variables' I have converted a SPSS file in long format (child/twin level) to wide format (family level). The variables that differ between the twin-members (child 1 is firstborn twin-member, child 2 is second-born twin-member) of one family ( e.g. handedness, IQ) now end at .1 for child 1 and .2 for child 2. For all those variables (string and numeric variables) I would like to rename them in order that .1 becomes _C1 and .2 becomes _C2. All variables names have different length but all end at '.1' or '.2'.
So for example, I have data concerning IQ and handedness of a twin pair: WISC_TIQ.1 and Handedness.1 which represents IQ and handedness of child 1, and WISC_TIQ.2 and Handedness.2 which represents IQ and handedness of child 2. Instead of renaming all those variables by hand, I wondered how to use loops to rename all these different variables ending at .1 into different variables all ending at _C1 and all variables ending at .2 into variables ending at _C2.
On https://stats.idre.ucla.edu/spss/code/a-few-spss-loops-for-renaming-variables-dynamically/, I read something about using a macro and loops for this, but as I am quite new to SPSS I fail to rewrite the examples on those sites to my issue.
Some suggest using the python extension, but unfortunately, the remote system of my university does not allow reïnstalling SPSS 25 to install the python plugin.
I hope someone can help me.
Hello Vibeke. I think that if you back up to where you used CASESTOVARS, but include AUTOFIX and SEPARATOR sub-commands, you can get what you want directly. Here is a small example to illustrate.

* Generate a small dataset to illustrate.
NEW FILE.
DATASET CLOSE ALL.
DATA LIST LIST / Family Twin Male RHand (4F2.0) IQ (F3.0).
BEGIN DATA
1 1 1 1 105
1 2 1 1 103
2 1 0 1 108
2 2 0 1 110
END DATA.

* Restructure from LONG to WIDE.
* Notice the use of the AUTOFIX
* and SEPARATOR sub-commands below.

SORT CASES BY Family Twin.
CASESTOVARS
/ID=Family
/INDEX=Twin
/AUTOFIX=NO
/SEPARATOR "_C"
/GROUPBY=VARIABLE.


I hope this helps.

Cheers,
Bruce
Rich Ulrich
2020-05-18 20:37:30 UTC
Permalink
On Mon, 18 May 2020 06:58:05 -0700 (PDT), Vibeke Nielsen
Post by Vibeke Nielsen
Using 'restructure cases into variables' I have converted a SPSS file in long format (child/twin level) to wide format (family level). The variables that differ between the twin-members (child 1 is firstborn twin-member, child 2 is second-born twin-member) of one family ( e.g. handedness, IQ) now end at .1 for child 1 and .2 for
child 2. For all those variables (string and numeric variables) I would like to rename them in order that .1 becomes _C1 and .2 becomes _C2. All variables names have different length but all end at '.1' or '.2'.
Post by Vibeke Nielsen
So for example, I have data concerning IQ and handedness of a twin pair: WISC_TIQ.1 and Handedness.1 which represents IQ and handedness of child 1, and WISC_TIQ.2 and Handedness.2 which represents IQ and handedness of child 2. Instead of renaming all those variables by hand, I wondered how to use loops to rename all these
different variables ending at .1 into different variables all ending at _C1 and all variables ending at .2 into variables ending at _C2.
Post by Vibeke Nielsen
On https://stats.idre.ucla.edu/spss/code/a-few-spss-loops-for-renaming-variables-dynamically/, I read something about using a macro and loops for this, but as I am quite new to SPSS I fail to rewrite the examples on those sites to my issue.
Some suggest using the python extension, but unfortunately, the remote system of my university does not allow reïnstalling SPSS 25 to install the python plugin.
I hope someone can help me.
Bruce gave you the better solution, if you have the file
available - create the desired names and never see the others.

Or, you can use Find and Replace in the syntax editor, writing
a small task to rename variables; Execute and Save .

To change the names of variables (from the manual) -
Use the command RENAME VARIABLES. An example would be
RENAME VARIABLES (V1 V2 V3 = NEW1 NEW2 NEW3).

So -
Write out Rename variables
( < insert variable list > =
< insert variable list a second time > ).

Use Find and Replace on the SECOND LIST only,
to change .1 to _C1 and .2 to _C2.
--
Rich Ulrich
Loading...