Hi,
Although I've read through the archive of messages, I can't get a mass rename of variables to work with syntax. I have an SPSS worksheet with hundreds of variables (the first is Q5.0 and the last is Q5.20, but there are all kinds of variable names in between). I need to rename every variable by adding a prefix of '2' so that Q5.0 becomes 2Q5.0, for example. Any help would be greatly appreciated. I am using SPSS 14. Zlatko. |
One solution is to export the list of variables to a text editor, and
construct the appropriate syntax: RENAME VARI ( Q5.0 Q5.1 = Q_2_5.0 Q_2_5.1 ) . I use excel to copy formulas that create the strings down a list of variable names. (A global find and replace in most text editors would also work in this case.) Note that variable names cannot start with a numeric character. --jim -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Zlatko Sent: Monday, September 04, 2006 9:57 PM To: [hidden email] Subject: Renaming variables - noncontinuous Hi, Although I've read through the archive of messages, I can't get a mass rename of variables to work with syntax. I have an SPSS worksheet with hundreds of variables (the first is Q5.0 and the last is Q5.20, but there are all kinds of variable names in between). I need to rename every variable by adding a prefix of '2' so that Q5.0 becomes 2Q5.0, for example. Any help would be greatly appreciated. I am using SPSS 14. Zlatko. |
In reply to this post by Zlatko-4
At 10:57 PM 9/4/2006, Zlatko wrote:
>I need to rename every variable by adding a prefix of '2' so that Q5.0 >becomes 2Q5.0, for example. [I have] hundreds of variables (the first >is Q5.0 and the last is Q5.20, but there are all kinds of variable >names in between). Of course you can't do exactly that: '2Q5.0' isn't a valid SPSS variable name. Given valid new variable names, there's a pretty good pre-14 way to do it in pure SPSS: . Write the variable list to a text file using OMS. . Read the names into an SPSS file, into a string variable. . From those, generate RENAME statements, and write them to an external file. . INCLUDE or INSERT that file. It does, you see, take OMS; I don't remember what release that came in. I might do it that way even in 14, if SPSS had something like SAS's PROC CONTENTS, to write a file's the data dictionary to an SPSS file. I still think omitting that was a mistake. But that ain't the way the ball is bouncing. Here's a Python solution, with some tracing in the Python code. It renames only variables with names beginning "Q5." Much of the Python code is more or less lifted from Raynald Levesque's book. DISPLAY VARIABLES. File Information |-----------------------------|---------------------------| |Output Created |06-SEP-2006 00:45:09 | |-----------------------------|---------------------------| Variable Information |--------|--------|------|---------------|------------|------------| |Variable|Position|Label |Measurement |Print Format|Write Format| | | | |Level | | | |--------|--------|------|---------------|------------|------------| |Q5.0 |1 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.1 |2 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.2 |3 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.3 |4 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.4 |5 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.5 |6 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR1 |7 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.6 |8 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.7 |9 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.8 |10 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.9 |11 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.10 |12 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR2 |13 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.11 |14 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.12 |15 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.13 |16 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.14 |17 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.15 |18 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.16 |19 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.17 |20 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.18 |21 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.19 |22 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.20 |23 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| Variables in the working file * Snakes on a computer! Python to, more or less, the rescue . BEGIN PROGRAM. import spss varcount=spss.GetVariableCount() varlist=[] for i in xrange(varcount): Command = "ECHO 'Variable name: "+ spss.GetVariableName(i) + "'." OldName = spss.GetVariableName(i) OldPfx = OldName[0:3] OldSfx = OldName[3:] print OldPfx, OldSfx if OldPfx == "Q5.": NewName = "R6." + OldSfx Command = "RENAME VARIABLES (" + OldName + "=" + NewName + ")." print Command spss.Submit(Command) END PROGRAM. Q5. 0 RENAME VARIABLES (Q5.0=R6.0). Q5. 1 RENAME VARIABLES (Q5.1=R6.1). Q5. 2 RENAME VARIABLES (Q5.2=R6.2). Q5. 3 RENAME VARIABLES (Q5.3=R6.3). Q5. 4 RENAME VARIABLES (Q5.4=R6.4). Q5. 5 RENAME VARIABLES (Q5.5=R6.5). XVA R1 Q5. 6 RENAME VARIABLES (Q5.6=R6.6). Q5. 7 RENAME VARIABLES (Q5.7=R6.7). Q5. 8 RENAME VARIABLES (Q5.8=R6.8). Q5. 9 RENAME VARIABLES (Q5.9=R6.9). Q5. 10 RENAME VARIABLES (Q5.10=R6.10). XVA R2 Q5. 11 RENAME VARIABLES (Q5.11=R6.11). Q5. 12 RENAME VARIABLES (Q5.12=R6.12). Q5. 13 RENAME VARIABLES (Q5.13=R6.13). Q5. 14 RENAME VARIABLES (Q5.14=R6.14). Q5. 15 RENAME VARIABLES (Q5.15=R6.15). Q5. 16 RENAME VARIABLES (Q5.16=R6.16). Q5. 17 RENAME VARIABLES (Q5.17=R6.17). Q5. 18 RENAME VARIABLES (Q5.18=R6.18). Q5. 19 RENAME VARIABLES (Q5.19=R6.19). Q5. 20 RENAME VARIABLES (Q5.20=R6.20). DISPLAY VARIABLES. File Information |-----------------------------|---------------------------| |Output Created |06-SEP-2006 00:45:10 | |-----------------------------|---------------------------| Variable Information |--------|--------|------|---------------|------------|------------| |Variable|Position|Label |Measurement |Print Format|Write Format| | | | |Level | | | |--------|--------|------|---------------|------------|------------| |R6.0 |1 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.1 |2 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.2 |3 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.3 |4 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.4 |5 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.5 |6 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR1 |7 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.6 |8 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.7 |9 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.8 |10 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.9 |11 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.10 |12 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR2 |13 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.11 |14 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.12 |15 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.13 |16 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.14 |17 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.15 |18 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.16 |19 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.17 |20 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.18 |21 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.19 |22 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.20 |23 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| Variables in the working file |
At 12:49 AM 9/6/2006, I wrote:
>Given valid new variable names, there's a pretty good pre-14 way to do >it in pure SPSS: >. Write the variable list to a .SAV file using OMS. >. From that file, generate RENAME statements, and write them to an >external file. >. INCLUDE or INSERT that file. >It does, you see, take OMS; I don't remember what release that came >in. > >I might do it that way even in 14, if SPSS had something like SAS's >PROC CONTENTS, to write a file's the data dictionary to an SPSS file. See post "Data dictionary in SPSS" that I'm sending with this one, using OMS to get exactly that effect. Here, then, is an all-SPSS solution. It uses OMS, so you need that. I've avoided all SPSS 14 features; otherwise, it could use datasets instead of the two scratch .SAV files. It's less direct than the Python: generated code is written out and read back, instead of being submitted directly. DISPLAY VARIABLES. File Information |-----------------------------|---------------------------| |Output Created |06-SEP-2006 14:14:06 | |-----------------------------|---------------------------| Variable Information |--------|--------|------|---------------|------------|------------| |Variable|Position|Label |Measurement |Print Format|Write Format| | | | |Level | | | |--------|--------|------|---------------|------------|------------| |Q5.0 |1 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.1 |2 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.2 |3 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.3 |4 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.4 |5 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.5 |6 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR1 |7 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.6 |8 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.7 |9 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.8 |10 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.9 |11 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.10 |12 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR2 |13 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.11 |14 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.12 |15 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.13 |16 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.14 |17 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.15 |18 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.16 |19 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.17 |20 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.18 |21 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.19 |22 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |Q5.20 |23 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| Variables in the working file * Save the working file; building the dictionare and the code . * needs a separate one . SAVE OUTFILE=SCRATCH. * ................................................................ . * Get the data dictionary as the working file . OMS /SELECT TABLES /IF COMMANDS=['File Information'] SUBTYPES=['Variable Information'] /DESTINATION FORMAT=SAV OUTFILE=ATTRIBS VIEWER=NO. . DISPLAY VARIABLES. File Information |-----------------------------|---------------------------| |Output Created |06-SEP-2006 14:14:06 | |-----------------------------|---------------------------| C:\Documents and Settings\Richard\My Documents\Temporary\SPSS \2006-09-04 Zlatko - Renaming variables - noncontinuous DATA.SAV OMSEND. GET FILE=ATTRIBS /DROP=COMMAND_ SUBTYPE_ LABEL_ /RENAME=(VAR1=NAME). * ................................................................ . * Write the RENAME code. I've made this code follow very closely . * the Python code of the previous posting, to illustrate some . * of the ways that Python works. . * OBSERVE: There's a loop in the Python code, but not here. Here, . * the loop is the SPSS implicit loop through cases in the file. . STRING COMMAND(A80). STRING #OldName #OldPfx #OldSfx #NewName (A8). * OldName = spss.GetVariableName(i) . * OldPfx = OldName[0:3] . * OldSfx = OldName[3:] . * print OldPfx, OldSfx . COMPUTE #OldName = NAME. COMPUTE #OldPfx = SUBSTR(#OldName,1,3). COMPUTE #OldSfx = SUBSTR(#OldName,4). PRINT / #OldPfx #OldSfx. * if OldPfx == "Q5.": . DO IF #OldPfx EQ "Q5.". * NewName = "R6." + OldSfx . * Command = "RENAME VARIABLES ("+ OldName+ "="+ NewName+ ")." . * print Command . . COMPUTE #NewName = CONCAT("R6.",#OldSfx). . COMPUTE COMMAND = CONCAT("RENAME VARIABLES (",#OldName, "=",#NewName, ")."). . PRINT / COMMAND. * spss.Submit(Command) . . WRITE OUTFILE=GEN_CODE / COMMAND. END IF. EXECUTE /* because the only output used is from WRITE */ . Q5. 0 RENAME VARIABLES (Q5.0 =R6.0 ). Q5. 1 RENAME VARIABLES (Q5.1 =R6.1 ). Q5. 2 RENAME VARIABLES (Q5.2 =R6.2 ). Q5. 3 RENAME VARIABLES (Q5.3 =R6.3 ). Q5. 4 RENAME VARIABLES (Q5.4 =R6.4 ). Q5. 5 RENAME VARIABLES (Q5.5 =R6.5 ). XVA R1 Q5. 6 RENAME VARIABLES (Q5.6 =R6.6 ). Q5. 7 RENAME VARIABLES (Q5.7 =R6.7 ). Q5. 8 RENAME VARIABLES (Q5.8 =R6.8 ). Q5. 9 RENAME VARIABLES (Q5.9 =R6.9 ). Q5. 10 RENAME VARIABLES (Q5.10 =R6.10 ). XVA R2 Q5. 11 RENAME VARIABLES (Q5.11 =R6.11 ). Q5. 12 RENAME VARIABLES (Q5.12 =R6.12 ). Q5. 13 RENAME VARIABLES (Q5.13 =R6.13 ). Q5. 14 RENAME VARIABLES (Q5.14 =R6.14 ). Q5. 15 RENAME VARIABLES (Q5.15 =R6.15 ). Q5. 16 RENAME VARIABLES (Q5.16 =R6.16 ). Q5. 17 RENAME VARIABLES (Q5.17 =R6.17 ). Q5. 18 RENAME VARIABLES (Q5.18 =R6.18 ). Q5. 19 RENAME VARIABLES (Q5.19 =R6.19 ). Q5. 20 RENAME VARIABLES (Q5.20 =R6.20 ). * ................................................................ . * Reload the data, and apply the RENAME commands. . GET FILE=SCRATCH. INCLUDE GEN_CODE. 120 RENAME VARIABLES (Q5.0 =R6.0 ). 121 RENAME VARIABLES (Q5.1 =R6.1 ). 122 RENAME VARIABLES (Q5.2 =R6.2 ). 123 RENAME VARIABLES (Q5.3 =R6.3 ). 124 RENAME VARIABLES (Q5.4 =R6.4 ). 125 RENAME VARIABLES (Q5.5 =R6.5 ). 126 RENAME VARIABLES (Q5.6 =R6.6 ). 127 RENAME VARIABLES (Q5.7 =R6.7 ). 128 RENAME VARIABLES (Q5.8 =R6.8 ). 129 RENAME VARIABLES (Q5.9 =R6.9 ). 130 RENAME VARIABLES (Q5.10 =R6.10 ). 131 RENAME VARIABLES (Q5.11 =R6.11 ). 132 RENAME VARIABLES (Q5.12 =R6.12 ). 133 RENAME VARIABLES (Q5.13 =R6.13 ). 134 RENAME VARIABLES (Q5.14 =R6.14 ). 135 RENAME VARIABLES (Q5.15 =R6.15 ). 136 RENAME VARIABLES (Q5.16 =R6.16 ). 137 RENAME VARIABLES (Q5.17 =R6.17 ). 138 RENAME VARIABLES (Q5.18 =R6.18 ). 139 RENAME VARIABLES (Q5.19 =R6.19 ). 140 RENAME VARIABLES (Q5.20 =R6.20 ). 141 142 * End of INSERT and INCLUDE nesting level 01. DISPLAY VARIABLES. File Information |--------------------------|---------------------------| |Output Created |06-SEP-2006 14:14:06 | |--------------------------|---------------------------| C:\Documents and Settings\Richard\My Documents\Temporary\SPSS \2006-09-04 Zlatko - Renaming variables - noncontinuous DATA.SAV Variable Information |--------|--------|------|---------------|------------|------------| |Variable|Position|Label |Measurement |Print Format|Write Format| | | | |Level | | | |--------|--------|------|---------------|------------|------------| |R6.0 |1 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.1 |2 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.2 |3 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.3 |4 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.4 |5 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.5 |6 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR1 |7 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.6 |8 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.7 |9 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.8 |10 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.9 |11 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.10 |12 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |XVAR2 |13 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.11 |14 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.12 |15 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.13 |16 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.14 |17 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.15 |18 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.16 |19 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.17 |20 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.18 |21 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.19 |22 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| |R6.20 |23 |<none>|Scale |F2 |F2 | |--------|--------|------|---------------|------------|------------| Variables in the working file |
Free forum by Nabble | Edit this page |