Rename variables

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

Rename variables

albert_sun
Hi,

I have the following situation which I d not know how to do it in SPSS syntax. I read a file from excel contains variables ID Name Gender Q1 K2 F3 .....G5 ; I want to rename the variables (Q1 to G5).

I know that if the variables are named like (Q1 to Q10), I can use "rename variables Q1 to Q10 = K1 to K10", but in the case I listed, how to do it?
 
Reply | Threaded
Open this post in threaded view
|

Re: Rename variables

Andy W
There is no problem. For rename the original list on the left hand side can use the TO convention, and this just specifies the variables within a particular range in the data file. The right hand list though will use the convention of sequential numeric suffixes to rename the variables though. You just need the lists to have the same number.

***************************************.
DATA LIST FREE / A B C.
BEGIN DATA
1 2 3
END DATA.

RENAME VARIABLES (A TO C = X1 TO X3).
***************************************.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Rename variables

albert_sun
Thanks, Andy.


Is there anyway to get the number of variables apart from python?

Regards


On Fri, Sep 19, 2014 at 1:49 AM, Andy W [via SPSSX Discussion] <[hidden email]> wrote:
There is no problem. For rename the original list on the left hand side can use the TO convention, and this just specifies the variables within a particular range in the data file. The right hand list though will use the convention of sequential numeric suffixes to rename the variables though. You just need the lists to have the same number.

***************************************.
DATA LIST FREE / A B C.
BEGIN DATA
1 2 3
END DATA.

RENAME VARIABLES (A TO C = X1 TO X3).
***************************************.


If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/Rename-variables-tp5727307p5727308.html
To unsubscribe from Rename variables, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Rename variables

Jignesh Sutar
If the number of variables is unknown it would be pretty tricky to try achieve what you are trying to do here (possible, probably with VARSTOCASES and WRITE ugly hack) but otherwise python would be a much better solution here.

Alternatively, if this is a one off, may be better off to rename manually, rather then setting up a dynamic approach.



On 19 September 2014 00:12, albert_sun <[hidden email]> wrote:
Thanks, Andy.


Is there anyway to get the number of variables apart from python?

Regards


On Fri, Sep 19, 2014 at 1:49 AM, Andy W [via SPSSX Discussion] <[hidden email]> wrote:
There is no problem. For rename the original list on the left hand side can use the TO convention, and this just specifies the variables within a particular range in the data file. The right hand list though will use the convention of sequential numeric suffixes to rename the variables though. You just need the lists to have the same number.

***************************************.
DATA LIST FREE / A B C.
BEGIN DATA
1 2 3
END DATA.

RENAME VARIABLES (A TO C = X1 TO X3).
***************************************.



If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/Rename-variables-tp5727307p5727308.html
To unsubscribe from Rename variables, click here.
NAML



View this message in context: Re: Rename variables

Sent from the SPSSX Discussion mailing list archive at Nabble.com.
===================== 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: Rename variables

Andy W
In reply to this post by albert_sun
Here is a python solution (I realized you asked not for python, but just for general reference for everyone, I have seen a similar questions come up before). This just grabs the meta-data, so is not a problem for large datasets.

Given you know the begin and end variable names, this code calculates the number of variables in between (no error checking to make sure the first is before the last).

***************************************.
DATA LIST FREE / A B D C E.
BEGIN DATA
1 2 3 4 5
END DATA.
*Python to figure out how many given begin and end.
BEGIN PROGRAM Python.
import spss
beg = 'A'
end = 'C'
varcount=spss.GetVariableCount()
for i in xrange(varcount):
  print i
  if spss.GetVariableName(i) == beg:
    f = i
  elif spss.GetVariableName(i) == end:
    l = i
    break
r = int(l - f + 1)
spss.SetMacroValue('!XEnd','X' + str(r))
END PROGRAM.
RENAME VARIABLES (A TO C = X1 TO !XEnd).
***************************************.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Rename variables

Rich Ulrich
I suggest that SPSS might add this to their TO-DO requests -
an option on RENAME that pads out the numerical suffix for
names in a list. 

RENAME [to be renamed]/ PADOUT= [newprefix](start number).

I put in a start-number as an argument so that the prefix might
end in a number without being used for the increment.

--
Rich Ulrich

> Date: Fri, 19 Sep 2014 06:42:49 -0700

> From: [hidden email]
> Subject: Re: Rename variables
> To: [hidden email]
>
> Here is a python solution (I realized you asked not for python, but just for
> general reference for everyone, I have seen a similar questions come up
> before). This just grabs the meta-data, so is not a problem for large
> datasets.
>
> Given you know the begin and end variable names, this code calculates the
> number of variables in between (no error checking to make sure the first is
> before the last).
>
> ***************************************.
> DATA LIST FREE / A B D C E.
> BEGIN DATA
> 1 2 3 4 5
> END DATA.
> *Python to figure out how many given begin and end.
> BEGIN PROGRAM Python.
> import spss
> beg = 'A'
> end = 'C'
> varcount=spss.GetVariableCount()
> for i in xrange(varcount):
> print i
> if spss.GetVariableName(i) == beg:
> f = i
> elif spss.GetVariableName(i) == end:
> l = i
> break
> r = int(l - f + 1)
> spss.SetMacroValue('!XEnd','X' + str(r))
> END PROGRAM.
> RENAME VARIABLES (A TO C = X1 TO !XEnd).
> ***************************************.
>
>
>
> -----
> Andy W
> [hidden email]
> http://andrewpwheeler.wordpress.com/
> --
> View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Rename-variables-tp5727307p5727320.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> 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: Rename variables

Jon K Peck
This seems to me to be an extremely rare need.  While it would be quite easy to make an extension command with this feature, my speculation is the number of people who would ever use this could be counted on one hand with fingers to spare.  And it is trivial to do this with a small dash of Python anyway, especially if the expand feature of the spssaux.VariableDict class is used.

If there is a ground swell of demand, I'd like to know about it.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Rich Ulrich <[hidden email]>
To:        [hidden email]
Date:        09/19/2014 01:34 PM
Subject:        Re: [SPSSX-L] Rename variables
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I suggest that SPSS might add this to their TO-DO requests -
an option on RENAME that pads out the numerical suffix for
names in a list.  

RENAME [to be renamed]/ PADOUT= [newprefix](start number).

I put in a start-number as an argument so that the prefix might
end in a number without being used for the increment.

--
Rich Ulrich

> Date: Fri, 19 Sep 2014 06:42:49 -0700
> From: [hidden email]
> Subject: Re: Rename variables
> To: [hidden email]
>
> Here is a python solution (I realized you asked not for python, but just for
> general reference for everyone, I have seen a similar questions come up
> before). This just grabs the meta-data, so is not a problem for large
> datasets.
>
> Given you know the begin and end variable names, this code calculates the
> number of variables in between (no error checking to make sure the first is
> before the last).
>
> ***************************************.
> DATA LIST FREE / A B D C E.
> BEGIN DATA
> 1 2 3 4 5
> END DATA.
> *Python to figure out how many given begin and end.
> BEGIN PROGRAM Python.
> import spss
> beg = 'A'
> end = 'C'
> varcount=spss.GetVariableCount()
> for i in xrange(varcount):
> print i
> if spss.GetVariableName(i) == beg:
> f = i
> elif spss.GetVariableName(i) == end:
> l = i
> break
> r = int(l - f + 1)
> spss.SetMacroValue('!XEnd','X' + str(r))
> END PROGRAM.
> RENAME VARIABLES (A TO C = X1 TO !XEnd).
> ***************************************.
>
>
>
> -----
> Andy W
> [hidden email]
>
http://andrewpwheeler.wordpress.com/
> --
> View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Rename-variables-tp5727307p5727320.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> 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 LISTSERV@... (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: Rename variables

Barry
I've come up with methods of doing bulk commands in Excel, using functions to create commands (what I call 'the poor man's macro').  An example is in the attached sheet.

You can write a large number of different syntax commands very quickly using these methods.
It's very good when you have a lot of variables, and have a hard time keeping track of them.

Barry

(this is my first time using Nabble with a file upload, so I don't know if it will work)

Example_of_writing_SPSS_commands_in_Excel.xlsx
Reply | Threaded
Open this post in threaded view
|

Re: Rename variables

Barry
In reply to this post by albert_sun