Yesterday question reworded

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Yesterday question reworded

Bruce Colton

Hello Everyone - I just realized how poorly worded yesterday's question was, so I'll now present it in a more understandable format.  In my spss dataset, I have a large number of variables that begin with the following 3 characters:  DA_

 

For example, I might have as variables:

 

DA_brt

DA_BART

DA_ AMC.EXC

DA_YAD

DA_AMCID

 

I have a need to rename these variables into:

 

brt

BART

AMC.EXC

YAD

AMCID

 

That is, just remove the DA_ from the beginning of each variable name to create the new variable name.  The handful of other variables in the dataset that don't begin with DA_, such as date and hour, remain unchanged. 

 

I know in python there is a way using regular expressions to select variables beginning with DA_, using (pattern=r'DA_) with VariableDict, but I need to change the name of these variables, not just select them.  I hope this makes my question more understandable, and I am grateful for any suggestions/help.  

Reply | Threaded
Open this post in threaded view
|

Re: Yesterday question reworded

Albert-Jan Roskam
Hi,

Untested, but probably correct:

import spss, spssaux
old = spssaux.GetVariableNamesList()
new = [v.lstrip("DA_") for v in old]
spss.Submit("rename variables (%s = %s)." % (" ".join(old), " ".join(new)))

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 4/29/10, Bruce Colton <[hidden email]> wrote:

From: Bruce Colton <[hidden email]>
Subject: [SPSSX-L] Yesterday question reworded
To: [hidden email]
Date: Thursday, April 29, 2010, 11:40 PM

Hello Everyone - I just realized how poorly worded yesterday's question was, so I'll now present it in a more understandable format.  In my spss dataset, I have a large number of variables that begin with the following 3 characters:  DA_

 

For example, I might have as variables:

 

DA_brt

DA_BART

DA_ AMC.EXC

DA_YAD

DA_AMCID

 

I have a need to rename these variables into:

 

brt

BART

AMC.EXC

YAD

AMCID

 

That is, just remove the DA_ from the beginning of each variable name to create the new variable name.  The handful of other variables in the dataset that don't begin with DA_, such as date and hour, remain unchanged. 

 

I know in python there is a way using regular expressions to select variables beginning with DA_, using (pattern=r'DA_) with VariableDict, but I need to change the name of these variables, not just select them.  I hope this makes my question more understandable, and I am grateful for any suggestions/help.