• Renaming varialbe names using loops

    From Vibeke Nielsen@21:1/5 to All on Mon May 18 06:58:05 2020
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bruce Weaver@21:1/5 to Vibeke Nielsen on Mon May 18 09:07:49 2020
    On Monday, May 18, 2020 at 9:58:08 AM UTC-4, Vibeke Nielsen wrote:
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich Ulrich@21:1/5 to nielsen.vl1997@gmail.com on Mon May 18 16:37:30 2020
    On Mon, 18 May 2020 06:58:05 -0700 (PDT), Vibeke Nielsen <nielsen.vl1997@gmail.com> wrote:

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)