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
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...)
'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 -
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
Kurt
> > > > Kurt- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
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 -