Renaming automatically variables variables in various data files

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

Renaming automatically variables variables in various data files

Carlos Renato (www.estatistico.org)
Dear friends of list

I have two files which have the same variables, but with diferent names in
each file.
I want to open each file and rename in each this variables automatically and
the
result is the seven files with the variables with equal names.

For example:

In file one:

q114.1
q114.1.1
q114.2.1
q114.3.1
q114.4.1
q114.5.1
q114.6a1
q114.6b1
q114.7.1
q114.8.1

In file two:

q114.2
q114.1.2
q114.2.2
q114.3.2
q114.4.2
q114.5.2
q114.6a2
q114.6b2
q114.7.2
q114.8.2

And the final output files:

Final file one and final file 2 have the same variables:

q114
q114.1
q114.1
q114.1
q114.1
q114.1
q114.6a
q114.6b
q114.7
q114.8

The objective is to make a merge of this files.

Thanks for all advances.

Carlos Renato
Statistician - Brazil

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Renaming automatically variables variables in various data files

Peck, Jon
In the final output request below by the OP, many variables have the same name.  Assuming that what was meant was to strip off the final part that differs across datasets, here is a little Python function that takes care of the renaming (you could do all this in the merging process using the RENAME subcommand, but that would be rather tedious.)

Here is the function.  It illustrates, once again, the power and clarity of regular expressions when processing text.  You would get or activate each dataset in turn, call the function, and save the result.  The looping part could be automated, but I'll just show the core functionality, since looping through the datasets manually isn't a big deal in this case.

Beware of list server line wrapping on the Submit line below: it is meant to be one line.

BEGIN PROGRAM.
# Rename variables so that names ending in one or more digits are stripped of the digits
#along with a preceding "." if any.

import spss, spssaux, re

def stripdigits():
    regexp = re.compile(r"\.*\d+$")
    oldnames = []
    newnames = []
    vardict = spssaux.VariableDict()
    for v in vardict:
        newname = re.sub(regexp, "", v.VariableName)
        if newname != v.VariableName:
            oldnames.append(v.VariableName)
            newnames.append(newname)
    if newnames:
        spss.Submit("RENAME VARIABLES (" + " ".join(oldnames) + "=" + " ".join(newnames) + ")")

stripdigits()
END PROGRAM.

The regular expression above (the regexp line) recognizes text with an optional "." followed by one or more digits followed by the end of the text.  That recognized pattern is removed by the
re.sub call.

This is written so that it will work back to SPSS 14 if the Python plug-in is installed.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Carlos Renato
Sent: Tuesday, September 16, 2008 7:40 AM
To: [hidden email]
Subject: [SPSSX-L] Renaming automatically variables variables in various data files

Dear friends of list

I have two files which have the same variables, but with diferent names in
each file.
I want to open each file and rename in each this variables automatically and
the
result is the seven files with the variables with equal names.

For example:

In file one:

q114.1
q114.1.1
q114.2.1
q114.3.1
q114.4.1
q114.5.1
q114.6a1
q114.6b1
q114.7.1
q114.8.1

In file two:

q114.2
q114.1.2
q114.2.2
q114.3.2
q114.4.2
q114.5.2
q114.6a2
q114.6b2
q114.7.2
q114.8.2

And the final output files:

Final file one and final file 2 have the same variables:

q114
q114.1
q114.1
q114.1
q114.1
q114.1
q114.6a
q114.6b
q114.7
q114.8

The objective is to make a merge of this files.

Thanks for all advances.

Carlos Renato
Statistician - Brazil

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Renaming automatically variables variables in various data files

Carlos Renato (www.estatistico.org)
Dear Peck

   Thanks...this solve my problem.

Very much thanks.

Carlos Renato
Statistician

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD