Discussion:
Add a suffix to all variable names
(too old to reply)
k***@cox.net
2007-01-24 18:31:19 UTC
Permalink
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
2007-01-24 20:17:58 UTC
Permalink
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.
Post by k***@cox.net
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...)
but I'm getting an error.
**************.
*** 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'.
*** 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
f***@lsc.gov.uk
2007-01-25 11:16:13 UTC
Permalink
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
Post by Jim
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.
Post by k***@cox.net
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...)
but I'm getting an error.
**************.
*** 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'.
*** 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- Hide quoted text -- Show quoted text -
JKPeck
2007-01-25 13:33:52 UTC
Permalink
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
Post by f***@lsc.gov.uk
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
Post by Jim
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.
Post by k***@cox.net
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...)
but I'm getting an error.
**************.
*** 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'.
*** 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- Hide quoted text -- Show quoted text -
k***@cox.net
2007-01-25 16:16:44 UTC
Permalink
Jon: The programmability approach worked flawlessly, and it can easily
be changed to prefix variables, too. Thanks.

Kurt
Post by JKPeck
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
Post by f***@lsc.gov.uk
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
Post by Jim
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.
Post by k***@cox.net
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...)
but I'm getting an error.
**************.
*** 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'.
*** 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- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
jafary
2007-01-28 21:03:37 UTC
Permalink
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
Post by k***@cox.net
Jon: The programmability approach worked flawlessly, and it can easily
be changed to prefix variables, too. Thanks.
Kurt
Post by JKPeck
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
Post by f***@lsc.gov.uk
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
Post by Jim
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.
Post by k***@cox.net
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...)
but I'm getting an error.
**************.
*** 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'.
*** 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- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Loading...