renaming variables - deleting the first part of the name

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

renaming variables - deleting the first part of the name

Zina Karapetyan
Dear List,

 

I have a file with over a 100 variables named Q101_ofcsp, Q102_ofceq,
Q103_clean, Q107_cereq, Q108_load, Q109_oppor, Q110_descr, etc. I need
to get rid of the QXXX_ part for every variable; that is to rename
Q101_ofcsp to ofcsp, etc. Is there a way to do this in SPSS besides
using RENAME VARIABLES? Any help would be greatly appreciated.

 

Thank you very much,

Zina

 

 
Reply | Threaded
Open this post in threaded view
|

Re: renaming variables - deleting the first part of the name

Lise Valentine
I don't know of another way through syntax, but if it is not much more
than 100 variables it shouldn't take too terribly long to just go in and
delete the first 5 characters of each by hand in the variable view.

Lise

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Zina Karapetyan
Sent: Thursday, September 21, 2006 11:54 AM
To: [hidden email]
Subject: renaming variables - deleting the first part of the name

Dear List,



I have a file with over a 100 variables named Q101_ofcsp, Q102_ofceq,
Q103_clean, Q107_cereq, Q108_load, Q109_oppor, Q110_descr, etc. I need
to get rid of the QXXX_ part for every variable; that is to rename
Q101_ofcsp to ofcsp, etc. Is there a way to do this in SPSS besides
using RENAME VARIABLES? Any help would be greatly appreciated.



Thank you very much,

Zina
Reply | Threaded
Open this post in threaded view
|

Re: renaming variables - deleting the first part of the name

King Douglas
In reply to this post by Zina Karapetyan
Zina,

  RENAME VARIABLES is your friend.  This is slightly modified and tested code from Ray Levesque's marvelous site
  http://www.spsstools.net

  Variable names were shortened for backward-compatibility with earlier versions of SPSS that did not permit long variable names.

  HTH

  King Douglas
  American Airlines

  *(Q) I am about to merge three huge data sets. Every dataset contains hundreds of
  vars. The var names are identical except for the prefix "tn". For example,
  var names of dataset no.1 start with"t1" t1abc, t1xyz, t1sps....
  * var names of dataset no.2 start with"t2" t2abc, t2xyz, t2sps....
  * var names of dataset no.3 start with"t3" t3abc, t3xyz, t3sps....
  * How can I rename the vars of datsets nos. 2 and 3, so that they will begin
  with "t1", e.g. "t1abc" (former "t3abc"), too?
  *(A) Posted to SPSSX-L by [hidden email] on 2002/01/23.
  DATA LIST FREE
  / Q101_ofX, Q102_ofY, Q103_cle, Q107_cer, Q108_loa, Q109_opp.
  BEGIN DATA
  85 95 5 87 100 1
  90 100 1 25 25 2
  END DATA.
  LIST.
  SAVE OUTFILE='c:\temp\mydata.sav'.
  * Be sure you have a backup copy of your data file before running this.
  * Next lines ensures no error will occur when there are more than 32,000 variables
  in the data file (Thanks to H. Maletta for this suggestion).
  N OF CASES 1.
  FLIP.
  STRING newname(A8).
  COMPUTE newname=SUBSTR(case_lbl,6).
  WRITE OUTFILE 'c:\temp\temp.sps'
  /"RENAME VARIABLE ("case_lbl"="newname").".
  EXE.
  GET FILE='c:\temp\mydata.sav'.
  INCLUDE 'c:\temp\temp.sps'.


Zina Karapetyan <[hidden email]> wrote:
  Dear List,



I have a file with over a 100 variables named Q101_ofcsp, Q102_ofceq,
Q103_clean, Q107_cereq, Q108_load, Q109_oppor, Q110_descr, etc. I need
to get rid of the QXXX_ part for every variable; that is to rename
Q101_ofcsp to ofcsp, etc. Is there a way to do this in SPSS besides
using RENAME VARIABLES? Any help would be greatly appreciated.



Thank you very much,

Zina
Reply | Threaded
Open this post in threaded view
|

Re: renaming variables - deleting the first part of the name

Richard Ristow
In reply to this post by Zina Karapetyan
At 12:53 PM 9/21/2006, Zina Karapetyan wrote:

>I have a file with over a 100 variables named Q101_ofcsp, Q102_ofceq,
>Q103_clean, Q107_cereq, Q108_load, Q109_oppor, Q110_descr, etc. I need
>to get rid of the QXXX_ part for every variable; that is to rename
>Q101_ofcsp to ofcsp, etc. Is there a way to do this in SPSS besides
>using RENAME VARIABLES? Any help would be greatly appreciated.

"Is there a way to do this in SPSS besides using RENAME VARIABLES?" No,
in syntax there isn't; either RENAME VARIABLES, or RENAME= on a SAVE,
GET, ADD FILES, or MATCH FILES. The question is (or, it may be), can
you generate those 100-odd RENAME specifications (like
"Q110_descr=descr") without writing them?

See
http://www.spsstools.net/Syntax/LabelsAndVariableNames/ChangeCharacterAtBeginningOfEachVarNames.txt
which does almost exactly what you want. It uses FLIP to get the
existing list of variables; I think that loses the string variables,
though.

Two other ways: if you have OMS (was that SPSS 12?), you can use it to
get the data dictionary (the list of variables), and generate the
RENAME specifications from it. See my posting "Data dictionary in
SPSS", Wed, 6 Sep 2006 13:30:05 -0400. That keeps all variables.

Or, if you have SPSS 14+, Python code (BEGIN PROGRAM/ END PROGRAM) has
direct access to the data dictionary with the variable namse. It has
string functions that are plenty good enough to break the name into
pieces and build the RENAME statement; and can run the result directly,
using its spss.submit function.

Post again, if this isn't enough information.

A thought about WHETHER to do it: It looks like the prefix "QXXX_" is
something like a question number from a questionnaire; the suffix is
mnemonic, descriptive of the question; and you want to keep just the
mnemonic part.

A lot of us, when we've a dataset with a lot of variables (100 is a
lot), prefer non-mnemonic variable names like "Q110". First, "Q110"
makes it clear where the variable is in the dataset, and in what
context it arises. (What were the adjacent questions? What section of
the questionnaire was it?) Second, "Q110" will often be easier to
remember, because there are usually several equally plausible
'mnemonic' names: "descr"? "descrip"? "item_dsc"? (Long names makes
this worse, not better.) I'd consider stripping the other way: Strip
"Q110_descr" to "Q110", with "descr" in the variable label.
Reply | Threaded
Open this post in threaded view
|

Re: renaming variables - deleting the first part of the name

Richard Ristow
At 02:06 PM 9/21/2006, Richard Ristow wrote:

>See
>http://www.spsstools.net/Syntax/LabelsAndVariableNames/ChangeCharacterAtBeginningOfEachVarNames.txt
>which does almost exactly what you want. It uses FLIP to get the
>existing list of variables; I think that loses the string variables,
>though.

Oh, I of little faith in Raynald Levesque! No, it doesn't lose string
variables. From the Command Syntax Reference entry for FLIP,

".. String variables in the original file are assigned system-missing
values after transposition."

So, this is probably what you want; see King Douglas's posting, that
gives it more completely. Remember the other tools too, though. And my
caveat, about whether it's a good idea.
Reply | Threaded
Open this post in threaded view
|

How to paste pivot cell text to excel using script?

peng jia
I want to paste the text of a few cells' of several
pivots into one Excel or notepad file.

Is there a way to do this using SPSS scripts
(preferred) or syntax?

Thank you!

Peng


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Reply | Threaded
Open this post in threaded view
|

Re: renaming variables - deleting the first part of the name

Peck, Jon
In reply to this post by Richard Ristow
Ok, here is the Python code.  First it defines a regular expression pattern matching a string starting with Q or q followed by any number of digits followed by "_"

Then it creates a Python copy of the SPSS variable dictionary including only variables whose name fits this pattern.

Next it loops over these variables creating new names deleting the pattern and submits rename commands.

Implicitly this assumes that there always IS a character after the "_" and that the resulting names are all distinct.  That could be dealt with easily with a little more code.

begin program.
import spss, spssaux, re

pat="^[Qq][0-9]+_"
vard = spssaux.VariableDict(pattern=pat)
for v in vard:
        newname = re.sub(pat, "", str(v))
        spss.Submit("RENAME VARIABLES (%s=%s)" % (v, newname))
end program.

At Directions in November, I will show regular expression search and replace functions hooked up so that they can be used like transformations on the case data with SPSS 15.

HTH,
Jon Peck
SPSS

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Richard Ristow
Sent: Thursday, September 21, 2006 1:06 PM
To: [hidden email]
Subject: Re: [SPSSX-L] renaming variables - deleting the first part of the name

At 12:53 PM 9/21/2006, Zina Karapetyan wrote:

>I have a file with over a 100 variables named Q101_ofcsp, Q102_ofceq,
>Q103_clean, Q107_cereq, Q108_load, Q109_oppor, Q110_descr, etc. I need
>to get rid of the QXXX_ part for every variable; that is to rename
>Q101_ofcsp to ofcsp, etc. Is there a way to do this in SPSS besides
>using RENAME VARIABLES? Any help would be greatly appreciated.

"Is there a way to do this in SPSS besides using RENAME VARIABLES?" No,
in syntax there isn't; either RENAME VARIABLES, or RENAME= on a SAVE,
GET, ADD FILES, or MATCH FILES. The question is (or, it may be), can
you generate those 100-odd RENAME specifications (like
"Q110_descr=descr") without writing them?

See
http://www.spsstools.net/Syntax/LabelsAndVariableNames/ChangeCharacterAtBeginningOfEachVarNames.txt
which does almost exactly what you want. It uses FLIP to get the
existing list of variables; I think that loses the string variables,
though.

Two other ways: if you have OMS (was that SPSS 12?), you can use it to
get the data dictionary (the list of variables), and generate the
RENAME specifications from it. See my posting "Data dictionary in
SPSS", Wed, 6 Sep 2006 13:30:05 -0400. That keeps all variables.

Or, if you have SPSS 14+, Python code (BEGIN PROGRAM/ END PROGRAM) has
direct access to the data dictionary with the variable namse. It has
string functions that are plenty good enough to break the name into
pieces and build the RENAME statement; and can run the result directly,
using its spss.submit function.

Post again, if this isn't enough information.

A thought about WHETHER to do it: It looks like the prefix "QXXX_" is
something like a question number from a questionnaire; the suffix is
mnemonic, descriptive of the question; and you want to keep just the
mnemonic part.

A lot of us, when we've a dataset with a lot of variables (100 is a
lot), prefer non-mnemonic variable names like "Q110". First, "Q110"
makes it clear where the variable is in the dataset, and in what
context it arises. (What were the adjacent questions? What section of
the questionnaire was it?) Second, "Q110" will often be easier to
remember, because there are usually several equally plausible
'mnemonic' names: "descr"? "descrip"? "item_dsc"? (Long names makes
this worse, not better.) I'd consider stripping the other way: Strip
"Q110_descr" to "Q110", with "descr" in the variable label.
Reply | Threaded
Open this post in threaded view
|

Re: How to paste pivot cell text to excel using script?

Sean McKenzie
In reply to this post by peng jia
This is along the lines of something I have been asking.

I am generating Tables like the below, but want to combine the elements from
these tables into a single table for each of My Offices (whicha re what the
below are), which means I want to write a particular cell from the table
below to a very particular cell in a very particular excell SS, and then, as
I update this data regularly, I will want to append existing tables.

Any thoughts?



                ProgramTypeCode
                        P2      P3      Total
                Count   Count   Count   Count
OfficeName      ANCHORAGE CHILD CARE    0       0       1       1
        CATHOLIC COMM. SVC.     0       25      298     323
        CORDOVA CHILD CARE      0       0       5       5
        FNSB CHILD CARE ASST    0       20      320     340
        HAINES/SKAGWAY  0       1       14      15
        HOMER CHILD CARE        0       12      47      59
        KENAI CHILD CARE        0       18      237     255
        KODIAK CHILD CARE       0       8       44      52
        MAT SU CHILD CARE       0       59      299     358
        METLAKATLA CHILD CAR    0       2       17      19
        MUNICIPALITY OF ANCH    0       135     1634    1769
        NOME, BARROW, KOTZ.,    0       0       7       7
        SEWARD CHILD CARE       0       1       26      27
        SITKA CHILD CARE        0       1       48      49
        VALDEZ CHILD CARE       0       1       23      24
        WRANGELL CHILD CARE     0       1       21      22
        Total   0       284     3041    3325




>From: peng jia <[hidden email]>
>Reply-To: peng jia <[hidden email]>
>To: [hidden email]
>Subject: How to paste pivot cell text to excel using script?
>Date: Thu, 21 Sep 2006 11:50:39 -0700
>
>I want to paste the text of a few cells' of several
>pivots into one Excel or notepad file.
>
>Is there a way to do this using SPSS scripts
>(preferred) or syntax?
>
>Thank you!
>
>Peng
>
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
Reply | Threaded
Open this post in threaded view
|

Re: renaming variables - deleting the first part of the name

Kooij, A.J. van der
In reply to this post by Lise Valentine
No need to do it by hand if you have Word or other editor that can edit columns. You can copy the names from variable view, paste in Word, select first 5 columns (press Alt while selecting) and delete, then copy and paste back into variable view.
 
Anita van der Kooij
Data Theory Group
Leiden University

________________________________

From: SPSSX(r) Discussion on behalf of Lise Valentine
Sent: Thu 21/09/2006 19:24
To: [hidden email]
Subject: Re: renaming variables - deleting the first part of the name



I don't know of another way through syntax, but if it is not much more
than 100 variables it shouldn't take too terribly long to just go in and
delete the first 5 characters of each by hand in the variable view.

Lise

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Zina Karapetyan
Sent: Thursday, September 21, 2006 11:54 AM
To: [hidden email]
Subject: renaming variables - deleting the first part of the name

Dear List,



I have a file with over a 100 variables named Q101_ofcsp, Q102_ofceq,
Q103_clean, Q107_cereq, Q108_load, Q109_oppor, Q110_descr, etc. I need
to get rid of the QXXX_ part for every variable; that is to rename
Q101_ofcsp to ofcsp, etc. Is there a way to do this in SPSS besides
using RENAME VARIABLES? Any help would be greatly appreciated.



Thank you very much,

Zina



**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
**********************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: renaming variables - deleting the first part of the name

Esther Fujiwara
In reply to this post by Peck, Jon
Hi Jon,

I just wanted to thank you for this 10-year old post:

begin program.
import spss, spssaux, re

pat="^[Qq][0-9]+_"
vard = spssaux.VariableDict(pattern=pat)
for v in vard:
        newname = re.sub(pat, "", str(v))
        spss.Submit("RENAME VARIABLES (%s=%s)" % (v, newname))
end program.


...you just made my life a whole lot easier (and faster).
Thank you!


Esther


--
Esther Fujiwara, Ph.D.
Associate Professor
Department of Psychiatry
University of Alberta
Edmonton, AB
Canada T6G 2V2