Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Add a suffix to all variable names

1,540 views
Skip to first unread message

khei...@cox.net

unread,
Jan 24, 2007, 1:31:19 PM1/24/07
to
I'm trying to add a suffix (e.g., '_99') to all my variable names. I
tried to use the syntax below (which I got from:
http://pages.infinit.net/rlevesqu/Syntax/LabelsAndVariableNames/Add_99AtEndOfAllVariableNames.txt)
but I'm getting an error.

Here's the syntax:

**************.
*** Add _99 to the end of existing variable names.
*** Of course existing variable names are assumed to have
*** at most 5 characters.
**************.
DATA LIST LIST /v1 vara varz a2345 a3456.
BEGIN DATA.
1 1 1 1 1
END DATA.

SAVE OUTFILE='c:\temp\mydata.sav'.
FLIP.

STRING newname(A8).
* The last letter of var names having 8 characters are deleted.
* unless this would result in a name duplication.
COMPUTE newname=CONCAT(RTRIM(case_lbl),'_99').
WRITE OUTFILE='c:\temp\rename.sps'
/ 'RENAME VARIABLE ('case_lbl'='newname').'.
Execute.

GET FILE='c:\temp\mydata.sav'.
INCLUDE 'C:\temp\rename.sps'.

***********

(Since I already have my dataset and variables in SPSS, I started the
syntax at the STRING command.)

When I run the syntax as is, I get an error on case_lbl (Incorrect
variable name ...). What does case_lbl represent, and what do I need to
change it to for the syntax to work. My dataset has approximately 100
variables, and I just need to suffix them with '_99'.

And as an aside, I'm confused over this comment:

*** Of course existing variable names are assumed to have
*** at most 5 characters.

But the datalist used in the example includes three variables (v1,
vara, varz) that have less than 5 characters.

Thanks for any help.

Kurt

Jim

unread,
Jan 24, 2007, 3:17:58 PM1/24/07
to
if I understand correctly, i would write a MATCH File file=* /rename
(var1 = var1_99), etc. would copy/paste var names from variable view
into a text editor and use a record macro to add parens and _99. kind
of brute force but don't know other way.

On Jan 24, 1:31 pm, kheisl...@cox.net wrote:
> I'm trying to add a suffix (e.g., '_99') to all my variable names. I

> tried to use the syntax below (which I got from:http://pages.infinit.net/rlevesqu/Syntax/LabelsAndVariableNames/Add_9...)

fred....@lsc.gov.uk

unread,
Jan 25, 2007, 6:16:13 AM1/25/07
to
You can do it via the following script

'BEGIN DESCRIPTION
'Add an extension to all variable names.
'END DESCRIPTION


Option Explicit

Sub Main
Const SUFFIX= "_99"

' get variables
Dim objDataDoc As ISpssDataDoc
Dim objDocuments As ISpssDocuments
Set objDocuments = objSpssApp.Documents

Dim varList As Variant, newlist As Variant, oldlist As Variant
Dim i As Long

' get the dictionary
Set objDataDoc = objDocuments.GetDataDoc(0)

' Get the variables
varList = objDataDoc.GetVariables (False)

' Iterate through the array of variables
For i = LBound(varList) To UBound(varList)
oldlist = oldlist & " " & varList(i)
newlist = newlist & " " & varList(i) & SUFFIX
Next i

objSpssApp.ExecuteCommands "RENAME VARIABLES (" & oldlist & "=" &
newlist & ").", _
False 'run cmd asynchronously

End Sub

> > Kurt- Hide quoted text -- Show quoted text -

JKPeck

unread,
Jan 25, 2007, 8:33:52 AM1/25/07
to
Here is an example of doing this using the programmability approach
introduced in SPSS 14.

begin program.
import spss, spssaux
varlist = spssaux.VariableDict().variables
vars99 = " ".join([varname + "_99" for varname in varlist])
spss.Submit("RENAME VARIABLES (" + " ".join(varlist) + "=" + vars99 +
")")
end program.


The varlist = line creates a list of the variables.
The next line creates a list of the new names.
Then the Submit line executes a RENAME VARIABLES command using the two
lists.

This approach generalizes to selecting the variables to operate on
using a TO-like range, variable type or other selection mechanisms. If
you change the varlist = line, for example, to this, you have the
equivalent of x TO y.
varlist = spssaux.VariableDict().range("x", "y")

Regards,
Jon Peck

khei...@cox.net

unread,
Jan 25, 2007, 11:16:44 AM1/25/07
to
Jon: The programmability approach worked flawlessly, and it can easily
be changed to prefix variables, too. Thanks.

Kurt

> > > > Kurt- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -

jafary

unread,
Jan 28, 2007, 4:03:37 PM1/28/07
to
In the same light is there a way to add a prefix (or suffix) to all
entries of ONE variable ? For example, for the ID variable is it
possible to rename 1,2,3,4,5 to sk1, sk2, sk3 etc etc. ?

Many thanks

Fahim H. Jafary
Aga Khan University Hospital
Karachi, Pakistan

> > > > > Kurt- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -

0 new messages