AW: rename all variables in a dataset

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

AW: rename all variables in a dataset

Mario Giesel
DEFINE !addsuffix (suffix = !TOKENS(1) / var = !CMDEND)
!DO !i !IN (!var)
RENAME VAR (!i = !CONCAT(!i,!UNQUOTE(!suffix))).
!DOEND
!ENDDEFINE.
SET MPRINT = ON.
NUMERIC old1 old2 old3 (F1).
!addsuffix suffix = new var = old1 old2 old3.
SET MPRINT = OFF
Good luck,
Mario



----- Ursprüngliche Mail ----
Von: Lorenzo Sandini <[hidden email]>
An: [hidden email]
Gesendet: Donnerstag, den 7. August 2008, 19:02:21 Uhr
Betreff: rename all variables in a dataset

Hello,

I a new at this so be patient please  :)

I need to rename all the variables in a couple of  datasets by adding a
short suffix to them. I was told in the comp.soft-sys.stat.spss
newsgroup that I could do it with a macro, but I am too new at this to
do it by myself.

Bruce Weaver proposed an elegant solution of stacking datasets, but I am
afraid  this would make things mode difficult for me later, as I have to
share this data with people  who know as little as me about SPSS. I
paste our  discussion below for reference.

From what I have read on R. Levesque's SPSS website, macros are really
powerful ways of manipulating data without writing novel-long syntaxes.
Still, I haven't found a way to do this. Could anyone offer me a scratch
in the back ?

Thanks !

Lorenzo


QUOTE:
=====================================
Lorenzo Sandini wrote:

> Hello,
>
> Can anyone tell me how to rename all variables in a set, for example
> appending a suffix at the end of every variable ?
>
> The same survey has been repeatedly filled by participants at regular
> intervals, and the answers to the repeatd questions have been saved
> into various tables of an Access database, with an identical name
> (question1, question2, question3, ....)
>
> I imported the various tables as separate datasets (questionnaire 3mo,
> 6mo, 9mo, etc...) and they all have the same variables, that I would
> like to join into one dataset with variables like :
>
> question1_3mo, question1_6mo, question1_9mo,
> question2_3mo, question2_6mo, question2_9mo, etc...
>
> This could be done by hand since the number of variable is reasonable,
> but I'd be happy to learn something new and apply it later in bigger
> datasets.
>
> Thanks for your help !
>
> Lorenzo

No doubt you could write a macro to do this.  But it might be simpler to
use ADD FILES to stack your individual data sets, then CASESTOVARS to
restructure from the long to wide format.  When stacking the files, use
the /IN subcommand to create flags for the different time points, and
then after merging all the files, compute an index variable to be used
by CASESTOVARS.  I don't have SPSS on this machine, so cannot generate
an example, but I think something along these lines should work.


ADD FILES
file = "C:\MyFolder\data3mo.sav" / in = flag3mo /
file = "C:\MyFolder\data6mo.sav" / in = flag6mo /
file = "C:\MyFolder\data9mo.sav" / in = flag9mo .
EXE.

do if flag3mo.
-  compute time = 3 .
else if flag6mo
-  compute time = 6 ..
else.
-  compute time = 9 .
end if.
exe.

sort cases by id time.
CASESTOVARS
id=idvar /
index = time /
drop {list of unneeded variables }.


--
Bruce Weaver
[hidden email]
www.angelfire.com/wv/bwhomedir
"When all else fails, RTFM."
===============================

=====================
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


__________________________________________________
Do You Yahoo!?
Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails.
http://mail.yahoo.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
Mario Giesel
Munich, Germany
Reply | Threaded
Open this post in threaded view
|

Re: AW: rename all variables in a dataset

Lorenzo Sandini
Thanks Mario,

Works perfectly. My datasets are *almost* identical in structure, so
that most variables are present in all datasets, but some variables are
specific to some of the datasets. For example questionnaires at 12
months, 24 months, 36 months etc.. contain substantially more questions
than 3 months, 6 months, 18 months, etc...,and some questions were only
asked at baseline or at the 5yr closing visit.

Is there a way to just rename ALL variables in a dataset without listing
them ? (not that producing a list is complicated, but...). Of course the
key variable will have to be returned to its original state. If not, no
worries, this already did the job very well for me.

Thanks again,
Lorenzo


Mario Giesel wrote:

> DEFINE !addsuffix (suffix = !TOKENS(1) / var = !CMDEND)
> !DO !i !IN (!var)
> RENAME VAR (!i = !CONCAT(!i,!UNQUOTE(!suffix))).
> !DOEND
> !ENDDEFINE.
> SET MPRINT = ON.
> NUMERIC old1 old2 old3 (F1).
> !addsuffix suffix = new var = old1 old2 old3.
> SET MPRINT = OFF
> Good luck,
> Mario
>
>
>
> ----- Ursprüngliche Mail ----
> Von: Lorenzo Sandini <[hidden email]>
> An: [hidden email]
> Gesendet: Donnerstag, den 7. August 2008, 19:02:21 Uhr
> Betreff: rename all variables in a dataset
>
> Hello,
>
> I a new at this so be patient please  :)
>
> I need to rename all the variables in a couple of  datasets by adding a
> short suffix to them. I was told in the comp.soft-sys.stat.spss
> newsgroup that I could do it with a macro, but I am too new at this to
> do it by myself.
>
> Bruce Weaver proposed an elegant solution of stacking datasets, but I am
> afraid  this would make things mode difficult for me later, as I have to
> share this data with people  who know as little as me about SPSS. I
> paste our  discussion below for reference.
>
> From what I have read on R. Levesque's SPSS website, macros are really
> powerful ways of manipulating data without writing novel-long syntaxes.
> Still, I haven't found a way to do this. Could anyone offer me a scratch
> in the back ?
>
> Thanks !
>
> Lorenzo
>
>
> QUOTE:
> =====================================
> Lorenzo Sandini wrote:
>
>> Hello,
>>
>> Can anyone tell me how to rename all variables in a set, for example
>> appending a suffix at the end of every variable ?
>>
>> The same survey has been repeatedly filled by participants at regular
>> intervals, and the answers to the repeatd questions have been saved
>> into various tables of an Access database, with an identical name
>> (question1, question2, question3, ....)
>>
>> I imported the various tables as separate datasets (questionnaire 3mo,
>> 6mo, 9mo, etc...) and they all have the same variables, that I would
>> like to join into one dataset with variables like :
>>
>> question1_3mo, question1_6mo, question1_9mo,
>> question2_3mo, question2_6mo, question2_9mo, etc...
>>
>> This could be done by hand since the number of variable is reasonable,
>> but I'd be happy to learn something new and apply it later in bigger
>> datasets.
>>
>> Thanks for your help !
>>
>> Lorenzo
>>
>
> No doubt you could write a macro to do this.  But it might be simpler to
> use ADD FILES to stack your individual data sets, then CASESTOVARS to
> restructure from the long to wide format.  When stacking the files, use
> the /IN subcommand to create flags for the different time points, and
> then after merging all the files, compute an index variable to be used
> by CASESTOVARS.  I don't have SPSS on this machine, so cannot generate
> an example, but I think something along these lines should work.
>
>
> ADD FILES
> file = "C:\MyFolder\data3mo.sav" / in = flag3mo /
> file = "C:\MyFolder\data6mo.sav" / in = flag6mo /
> file = "C:\MyFolder\data9mo.sav" / in = flag9mo .
> EXE.
>
> do if flag3mo.
> -  compute time = 3 .
> else if flag6mo
> -  compute time = 6 ..
> else.
> -  compute time = 9 .
> end if.
> exe.
>
> sort cases by id time.
> CASESTOVARS
> id=idvar /
> index = time /
> drop {list of unneeded variables }.
>
>
> --
> Bruce Weaver
> [hidden email]
> www.angelfire.com/wv/bwhomedir
> "When all else fails, RTFM."
> ===============================
>
> =====================
> 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
>
>
> __________________________________________________
> Do You Yahoo!?
> Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails.
> http://mail.yahoo.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: AW: rename all variables in a dataset

Albert-Jan Roskam
Hi Lorenzo,

The following simple solution requires that the Python plugin is installed.
It adds the suffix "quest1_" to every variable that is not called "keyvar". I understood you had to retain the name of on key variable, right? Make sure the indentation of the program is correct (the listserv might mangle it), or else it won't work.

Cheers!!
Albert-Jan


begin program.
import spss
for var in range (spss.GetVariableCount()):
        varlist = spss.GetVariableName(var)
        if var <> "keyvar":
                spss.Submit("rename variables (%s = quest1_%s)." % (varlist, varlist))
end program.


--- On Fri, 8/8/08, Lorenzo Sandini <[hidden email]> wrote:

> From: Lorenzo Sandini <[hidden email]>
> Subject: Re: AW: rename all variables in a dataset
> To: [hidden email]
> Date: Friday, August 8, 2008, 6:45 AM
> Thanks Mario,
>
> Works perfectly. My datasets are *almost* identical in
> structure, so
> that most variables are present in all datasets, but some
> variables are
> specific to some of the datasets. For example
> questionnaires at 12
> months, 24 months, 36 months etc.. contain substantially
> more questions
> than 3 months, 6 months, 18 months, etc...,and some
> questions were only
> asked at baseline or at the 5yr closing visit.
>
> Is there a way to just rename ALL variables in a dataset
> without listing
> them ? (not that producing a list is complicated, but...).
> Of course the
> key variable will have to be returned to its original
> state. If not, no
> worries, this already did the job very well for me.
>
> Thanks again,
> Lorenzo
>
>
> Mario Giesel wrote:
> > DEFINE !addsuffix (suffix = !TOKENS(1) / var =
> !CMDEND)
> > !DO !i !IN (!var)
> > RENAME VAR (!i = !CONCAT(!i,!UNQUOTE(!suffix))).
> > !DOEND
> > !ENDDEFINE.
> > SET MPRINT = ON.
> > NUMERIC old1 old2 old3 (F1).
> > !addsuffix suffix = new var = old1 old2 old3.
> > SET MPRINT = OFF
> > Good luck,
> > Mario
> >
> >
> >
> > ----- Ursprüngliche Mail ----
> > Von: Lorenzo Sandini
> <[hidden email]>
> > An: [hidden email]
> > Gesendet: Donnerstag, den 7. August 2008, 19:02:21 Uhr
> > Betreff: rename all variables in a dataset
> >
> > Hello,
> >
> > I a new at this so be patient please  :)
> >
> > I need to rename all the variables in a couple of
> datasets by adding a
> > short suffix to them. I was told in the
> comp.soft-sys.stat.spss
> > newsgroup that I could do it with a macro, but I am
> too new at this to
> > do it by myself.
> >
> > Bruce Weaver proposed an elegant solution of stacking
> datasets, but I am
> > afraid  this would make things mode difficult for me
> later, as I have to
> > share this data with people  who know as little as me
> about SPSS. I
> > paste our  discussion below for reference.
> >
> > From what I have read on R. Levesque's SPSS
> website, macros are really
> > powerful ways of manipulating data without writing
> novel-long syntaxes.
> > Still, I haven't found a way to do this. Could
> anyone offer me a scratch
> > in the back ?
> >
> > Thanks !
> >
> > Lorenzo
> >
> >
> > QUOTE:
> > =====================================
> > Lorenzo Sandini wrote:
> >
> >> Hello,
> >>
> >> Can anyone tell me how to rename all variables in
> a set, for example
> >> appending a suffix at the end of every variable ?
> >>
> >> The same survey has been repeatedly filled by
> participants at regular
> >> intervals, and the answers to the repeatd
> questions have been saved
> >> into various tables of an Access database, with an
> identical name
> >> (question1, question2, question3, ....)
> >>
> >> I imported the various tables as separate datasets
> (questionnaire 3mo,
> >> 6mo, 9mo, etc...) and they all have the same
> variables, that I would
> >> like to join into one dataset with variables like
> :
> >>
> >> question1_3mo, question1_6mo, question1_9mo,
> >> question2_3mo, question2_6mo, question2_9mo,
> etc...
> >>
> >> This could be done by hand since the number of
> variable is reasonable,
> >> but I'd be happy to learn something new and
> apply it later in bigger
> >> datasets.
> >>
> >> Thanks for your help !
> >>
> >> Lorenzo
> >>
> >
> > No doubt you could write a macro to do this.  But it
> might be simpler to
> > use ADD FILES to stack your individual data sets, then
> CASESTOVARS to
> > restructure from the long to wide format.  When
> stacking the files, use
> > the /IN subcommand to create flags for the different
> time points, and
> > then after merging all the files, compute an index
> variable to be used
> > by CASESTOVARS.  I don't have SPSS on this
> machine, so cannot generate
> > an example, but I think something along these lines
> should work.
> >
> >
> > ADD FILES
> > file = "C:\MyFolder\data3mo.sav" /
> in = flag3mo /
> > file = "C:\MyFolder\data6mo.sav" /
> in = flag6mo /
> > file = "C:\MyFolder\data9mo.sav" /
> in = flag9mo .
> > EXE.
> >
> > do if flag3mo.
> > -  compute time = 3 .
> > else if flag6mo
> > -  compute time = 6 ..
> > else.
> > -  compute time = 9 .
> > end if.
> > exe.
> >
> > sort cases by id time.
> > CASESTOVARS
> > id=idvar /
> > index = time /
> > drop {list of unneeded variables }.
> >
> >
> > --
> > Bruce Weaver
> > [hidden email]
> > www.angelfire.com/wv/bwhomedir
> > "When all else fails, RTFM."
> > ===============================
> >
> > =====================
> > 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
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Sie sind Spam leid? Yahoo! Mail verfügt über einen
> herausragenden Schutz gegen Massenmails.
> > http://mail.yahoo.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

=====================
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: AW: rename all variables in a dataset

Lorenzo Sandini
Beautiful, thank you !

This does the trick except for one thing, my key variable gets the
suffix too. I replaced "keyvar" with the name of my key variable of course.

Having no knowledge of python, it is difficult for me to try to make
modifications. Also tried to replace <> with ne, but all variables get
the suffix, key variable included. Strange.

Anyway, your help is appreciated  :)

Lorenzo

Albert-jan Roskam wrote:

> Hi Lorenzo,
>
> The following simple solution requires that the Python plugin is installed.
> It adds the suffix "quest1_" to every variable that is not called "keyvar". I understood you had to retain the name of on key variable, right? Make sure the indentation of the program is correct (the listserv might mangle it), or else it won't work.
>
> Cheers!!
> Albert-Jan
>
>
> begin program.
> import spss
> for var in range (spss.GetVariableCount()):
>         varlist = spss.GetVariableName(var)
>         if var <> "keyvar":
>                 spss.Submit("rename variables (%s = quest1_%s)." % (varlist, varlist))
> end program.
>
>
> --- On Fri, 8/8/08, Lorenzo Sandini <[hidden email]> wrote:
>
>
>> From: Lorenzo Sandini <[hidden email]>
>> Subject: Re: AW: rename all variables in a dataset
>> To: [hidden email]
>> Date: Friday, August 8, 2008, 6:45 AM
>> Thanks Mario,
>>
>> Works perfectly. My datasets are *almost* identical in
>> structure, so
>> that most variables are present in all datasets, but some
>> variables are
>> specific to some of the datasets. For example
>> questionnaires at 12
>> months, 24 months, 36 months etc.. contain substantially
>> more questions
>> than 3 months, 6 months, 18 months, etc...,and some
>> questions were only
>> asked at baseline or at the 5yr closing visit.
>>
>> Is there a way to just rename ALL variables in a dataset
>> without listing
>> them ? (not that producing a list is complicated, but...).
>> Of course the
>> key variable will have to be returned to its original
>> state. If not, no
>> worries, this already did the job very well for me.
>>
>> Thanks again,
>> Lorenzo
>>
>>
>> Mario Giesel wrote:
>>
>>> DEFINE !addsuffix (suffix = !TOKENS(1) / var =
>>>
>> !CMDEND)
>>
>>> !DO !i !IN (!var)
>>> RENAME VAR (!i = !CONCAT(!i,!UNQUOTE(!suffix))).
>>> !DOEND
>>> !ENDDEFINE.
>>> SET MPRINT = ON.
>>> NUMERIC old1 old2 old3 (F1).
>>> !addsuffix suffix = new var = old1 old2 old3.
>>> SET MPRINT = OFF
>>> Good luck,
>>> Mario
>>>
>>>
>>>
>>> ----- Ursprüngliche Mail ----
>>> Von: Lorenzo Sandini
>>>
>> <[hidden email]>
>>
>>> An: [hidden email]
>>> Gesendet: Donnerstag, den 7. August 2008, 19:02:21 Uhr
>>> Betreff: rename all variables in a dataset
>>>
>>> Hello,
>>>
>>> I a new at this so be patient please  :)
>>>
>>> I need to rename all the variables in a couple of
>>>
>> datasets by adding a
>>
>>> short suffix to them. I was told in the
>>>
>> comp.soft-sys.stat.spss
>>
>>> newsgroup that I could do it with a macro, but I am
>>>
>> too new at this to
>>
>>> do it by myself.
>>>
>>> Bruce Weaver proposed an elegant solution of stacking
>>>
>> datasets, but I am
>>
>>> afraid  this would make things mode difficult for me
>>>
>> later, as I have to
>>
>>> share this data with people  who know as little as me
>>>
>> about SPSS. I
>>
>>> paste our  discussion below for reference.
>>>
>>> From what I have read on R. Levesque's SPSS
>>>
>> website, macros are really
>>
>>> powerful ways of manipulating data without writing
>>>
>> novel-long syntaxes.
>>
>>> Still, I haven't found a way to do this. Could
>>>
>> anyone offer me a scratch
>>
>>> in the back ?
>>>
>>> Thanks !
>>>
>>> Lorenzo
>>>
>>>
>>> QUOTE:
>>> =====================================
>>> Lorenzo Sandini wrote:
>>>
>>>
>>>> Hello,
>>>>
>>>> Can anyone tell me how to rename all variables in
>>>>
>> a set, for example
>>
>>>> appending a suffix at the end of every variable ?
>>>>
>>>> The same survey has been repeatedly filled by
>>>>
>> participants at regular
>>
>>>> intervals, and the answers to the repeatd
>>>>
>> questions have been saved
>>
>>>> into various tables of an Access database, with an
>>>>
>> identical name
>>
>>>> (question1, question2, question3, ....)
>>>>
>>>> I imported the various tables as separate datasets
>>>>
>> (questionnaire 3mo,
>>
>>>> 6mo, 9mo, etc...) and they all have the same
>>>>
>> variables, that I would
>>
>>>> like to join into one dataset with variables like
>>>>
>> :
>>
>>>> question1_3mo, question1_6mo, question1_9mo,
>>>> question2_3mo, question2_6mo, question2_9mo,
>>>>
>> etc...
>>
>>>> This could be done by hand since the number of
>>>>
>> variable is reasonable,
>>
>>>> but I'd be happy to learn something new and
>>>>
>> apply it later in bigger
>>
>>>> datasets.
>>>>
>>>> Thanks for your help !
>>>>
>>>> Lorenzo
>>>>
>>>>
>>> No doubt you could write a macro to do this.  But it
>>>
>> might be simpler to
>>
>>> use ADD FILES to stack your individual data sets, then
>>>
>> CASESTOVARS to
>>
>>> restructure from the long to wide format.  When
>>>
>> stacking the files, use
>>
>>> the /IN subcommand to create flags for the different
>>>
>> time points, and
>>
>>> then after merging all the files, compute an index
>>>
>> variable to be used
>>
>>> by CASESTOVARS.  I don't have SPSS on this
>>>
>> machine, so cannot generate
>>
>>> an example, but I think something along these lines
>>>
>> should work.
>>
>>> ADD FILES
>>> file = "C:\MyFolder\data3mo.sav" /
>>>
>> in = flag3mo /
>>
>>> file = "C:\MyFolder\data6mo.sav" /
>>>
>> in = flag6mo /
>>
>>> file = "C:\MyFolder\data9mo.sav" /
>>>
>> in = flag9mo .
>>
>>> EXE.
>>>
>>> do if flag3mo.
>>> -  compute time = 3 .
>>> else if flag6mo
>>> -  compute time = 6 ..
>>> else.
>>> -  compute time = 9 .
>>> end if.
>>> exe.
>>>
>>> sort cases by id time.
>>> CASESTOVARS
>>> id=idvar /
>>> index = time /
>>> drop {list of unneeded variables }.
>>>
>>>
>>> --
>>> Bruce Weaver
>>> [hidden email]
>>> www.angelfire.com/wv/bwhomedir
>>> "When all else fails, RTFM."
>>> ===============================
>>>
>>> =====================
>>> 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
>>>
>>>
>>> __________________________________________________
>>> Do You Yahoo!?
>>> Sie sind Spam leid? Yahoo! Mail verfügt über einen
>>>
>> herausragenden Schutz gegen Massenmails.
>>
>>> http://mail.yahoo.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
>>
>
> =====================
> 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: AW: rename all variables in a dataset

Albert-Jan Roskam
Hi Lorenzo,

You're welcome! I should have used 'varlist', not 'var', sorry. Here's a better version. I also extended it a little; now it's almost the same as an spss macro ('def' instead of 'define-!enddefine').

Cheers!!
Albert-Jan

begin program.
import spss
def addprefix (in_file, prefix):
        spss.Submit("get file = '%s'." % (in_file))
        for var in range (spss.GetVariableCount()):
                varlist = spss.GetVariableName(var)
                if varlist <> "keyvar":
                        spss.Submit("rename variables (%s = %s_%s)." % (varlist, prefix, varlist))
        out_file = in_file[:-4] + "_renamed.sav"
        spss.Submit("save outfile = '%s'." % (out_file))
# make sure you use FORWARD slashes!
addprefix (in_file = "d:/temp/somefile.sav", prefix="question1")
addprefix (in_file = "d:/temp/somefile2.sav", prefix="question2")
end program.




--- On Fri, 8/8/08, Lorenzo Sandini <[hidden email]> wrote:

> From: Lorenzo Sandini <[hidden email]>
> Subject: Re: AW: rename all variables in a dataset
> To: [hidden email]
> Date: Friday, August 8, 2008, 2:30 PM
> Beautiful, thank you !
>
> This does the trick except for one thing, my key variable
> gets the
> suffix too. I replaced "keyvar" with the name of
> my key variable of course.
>
> Having no knowledge of python, it is difficult for me to
> try to make
> modifications. Also tried to replace <> with ne, but
> all variables get
> the suffix, key variable included. Strange.
>
> Anyway, your help is appreciated  :)
>
> Lorenzo
>
> Albert-jan Roskam wrote:
> > Hi Lorenzo,
> >
> > The following simple solution requires that the Python
> plugin is installed.
> > It adds the suffix "quest1_" to every
> variable that is not called "keyvar". I understood
> you had to retain the name of on key variable, right? Make
> sure the indentation of the program is correct (the listserv
> might mangle it), or else it won't work.
> >
> > Cheers!!
> > Albert-Jan
> >
> >
> > begin program.
> > import spss
> > for var in range (spss.GetVariableCount()):
> >         varlist = spss.GetVariableName(var)
> >         if var <> "keyvar":
> >                 spss.Submit("rename variables (%s
> = quest1_%s)." % (varlist, varlist))
> > end program.
> >
> >
> > --- On Fri, 8/8/08, Lorenzo Sandini
> <[hidden email]> wrote:
> >
> >
> >> From: Lorenzo Sandini
> <[hidden email]>
> >> Subject: Re: AW: rename all variables in a dataset
> >> To: [hidden email]
> >> Date: Friday, August 8, 2008, 6:45 AM
> >> Thanks Mario,
> >>
> >> Works perfectly. My datasets are *almost*
> identical in
> >> structure, so
> >> that most variables are present in all datasets,
> but some
> >> variables are
> >> specific to some of the datasets. For example
> >> questionnaires at 12
> >> months, 24 months, 36 months etc.. contain
> substantially
> >> more questions
> >> than 3 months, 6 months, 18 months, etc...,and
> some
> >> questions were only
> >> asked at baseline or at the 5yr closing visit.
> >>
> >> Is there a way to just rename ALL variables in a
> dataset
> >> without listing
> >> them ? (not that producing a list is complicated,
> but...).
> >> Of course the
> >> key variable will have to be returned to its
> original
> >> state. If not, no
> >> worries, this already did the job very well for
> me.
> >>
> >> Thanks again,
> >> Lorenzo
> >>
> >>
> >> Mario Giesel wrote:
> >>
> >>> DEFINE !addsuffix (suffix = !TOKENS(1) / var =
> >>>
> >> !CMDEND)
> >>
> >>> !DO !i !IN (!var)
> >>> RENAME VAR (!i =
> !CONCAT(!i,!UNQUOTE(!suffix))).
> >>> !DOEND
> >>> !ENDDEFINE.
> >>> SET MPRINT = ON.
> >>> NUMERIC old1 old2 old3 (F1).
> >>> !addsuffix suffix = new var = old1 old2 old3.
> >>> SET MPRINT = OFF
> >>> Good luck,
> >>> Mario
> >>>
> >>>
> >>>
> >>> ----- Ursprüngliche Mail ----
> >>> Von: Lorenzo Sandini
> >>>
> >> <[hidden email]>
> >>
> >>> An: [hidden email]
> >>> Gesendet: Donnerstag, den 7. August 2008,
> 19:02:21 Uhr
> >>> Betreff: rename all variables in a dataset
> >>>
> >>> Hello,
> >>>
> >>> I a new at this so be patient please  :)
> >>>
> >>> I need to rename all the variables in a couple
> of
> >>>
> >> datasets by adding a
> >>
> >>> short suffix to them. I was told in the
> >>>
> >> comp.soft-sys.stat.spss
> >>
> >>> newsgroup that I could do it with a macro, but
> I am
> >>>
> >> too new at this to
> >>
> >>> do it by myself.
> >>>
> >>> Bruce Weaver proposed an elegant solution of
> stacking
> >>>
> >> datasets, but I am
> >>
> >>> afraid  this would make things mode difficult
> for me
> >>>
> >> later, as I have to
> >>
> >>> share this data with people  who know as
> little as me
> >>>
> >> about SPSS. I
> >>
> >>> paste our  discussion below for reference.
> >>>
> >>> From what I have read on R. Levesque's
> SPSS
> >>>
> >> website, macros are really
> >>
> >>> powerful ways of manipulating data without
> writing
> >>>
> >> novel-long syntaxes.
> >>
> >>> Still, I haven't found a way to do this.
> Could
> >>>
> >> anyone offer me a scratch
> >>
> >>> in the back ?
> >>>
> >>> Thanks !
> >>>
> >>> Lorenzo
> >>>
> >>>
> >>> QUOTE:
> >>> =====================================
> >>> Lorenzo Sandini wrote:
> >>>
> >>>
> >>>> Hello,
> >>>>
> >>>> Can anyone tell me how to rename all
> variables in
> >>>>
> >> a set, for example
> >>
> >>>> appending a suffix at the end of every
> variable ?
> >>>>
> >>>> The same survey has been repeatedly filled
> by
> >>>>
> >> participants at regular
> >>
> >>>> intervals, and the answers to the repeatd
> >>>>
> >> questions have been saved
> >>
> >>>> into various tables of an Access database,
> with an
> >>>>
> >> identical name
> >>
> >>>> (question1, question2, question3, ....)
> >>>>
> >>>> I imported the various tables as separate
> datasets
> >>>>
> >> (questionnaire 3mo,
> >>
> >>>> 6mo, 9mo, etc...) and they all have the
> same
> >>>>
> >> variables, that I would
> >>
> >>>> like to join into one dataset with
> variables like
> >>>>
> >> :
> >>
> >>>> question1_3mo, question1_6mo,
> question1_9mo,
> >>>> question2_3mo, question2_6mo,
> question2_9mo,
> >>>>
> >> etc...
> >>
> >>>> This could be done by hand since the
> number of
> >>>>
> >> variable is reasonable,
> >>
> >>>> but I'd be happy to learn something
> new and
> >>>>
> >> apply it later in bigger
> >>
> >>>> datasets.
> >>>>
> >>>> Thanks for your help !
> >>>>
> >>>> Lorenzo
> >>>>
> >>>>
> >>> No doubt you could write a macro to do this.
> But it
> >>>
> >> might be simpler to
> >>
> >>> use ADD FILES to stack your individual data
> sets, then
> >>>
> >> CASESTOVARS to
> >>
> >>> restructure from the long to wide format.
> When
> >>>
> >> stacking the files, use
> >>
> >>> the /IN subcommand to create flags for the
> different
> >>>
> >> time points, and
> >>
> >>> then after merging all the files, compute an
> index
> >>>
> >> variable to be used
> >>
> >>> by CASESTOVARS.  I don't have SPSS on this
> >>>
> >> machine, so cannot generate
> >>
> >>> an example, but I think something along these
> lines
> >>>
> >> should work.
> >>
> >>> ADD FILES
> >>> file =
> "C:\MyFolder\data3mo.sav" /
> >>>
> >> in = flag3mo /
> >>
> >>> file =
> "C:\MyFolder\data6mo.sav" /
> >>>
> >> in = flag6mo /
> >>
> >>> file =
> "C:\MyFolder\data9mo.sav" /
> >>>
> >> in = flag9mo .
> >>
> >>> EXE.
> >>>
> >>> do if flag3mo.
> >>> -  compute time = 3 .
> >>> else if flag6mo
> >>> -  compute time = 6 ..
> >>> else.
> >>> -  compute time = 9 .
> >>> end if.
> >>> exe.
> >>>
> >>> sort cases by id time.
> >>> CASESTOVARS
> >>> id=idvar /
> >>> index = time /
> >>> drop {list of unneeded variables }.
> >>>
> >>>
> >>> --
> >>> Bruce Weaver
> >>> [hidden email]
> >>> www.angelfire.com/wv/bwhomedir
> >>> "When all else fails, RTFM."
> >>> ===============================
> >>>
> >>> =====================
> >>> 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
> >>>
> >>>
> >>>
> __________________________________________________
> >>> Do You Yahoo!?
> >>> Sie sind Spam leid? Yahoo! Mail verfügt über
> einen
> >>>
> >> herausragenden Schutz gegen Massenmails.
> >>
> >>> http://mail.yahoo.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
> >>
> >
> > =====================
> > 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

=====================
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: AW: rename all variables in a dataset

vlad simion
In reply to this post by Lorenzo Sandini
Hi Lorenzo,

If you don't want to use macros or Python, there is a pretty nice solution
from Jon Peck posted on Ray's site in the scripts section:
http://www.spsstools.net/Scripts/Labels/AddExtensionToAllVariableNames.txt
All the best,
Vlad
On Fri, Aug 8, 2008 at 3:30 PM, Lorenzo Sandini
<[hidden email]>wrote:

> Beautiful, thank you !
>
> This does the trick except for one thing, my key variable gets the
> suffix too. I replaced "keyvar" with the name of my key variable of course.
>
> Having no knowledge of python, it is difficult for me to try to make
> modifications. Also tried to replace <> with ne, but all variables get
> the suffix, key variable included. Strange.
>
> Anyway, your help is appreciated  :)
>
> Lorenzo
>
>
> Albert-jan Roskam wrote:
>
>> Hi Lorenzo,
>>
>> The following simple solution requires that the Python plugin is
>> installed.
>> It adds the suffix "quest1_" to every variable that is not called
>> "keyvar". I understood you had to retain the name of on key variable, right?
>> Make sure the indentation of the program is correct (the listserv might
>> mangle it), or else it won't work.
>>
>> Cheers!!
>> Albert-Jan
>>
>>
>> begin program.
>> import spss
>> for var in range (spss.GetVariableCount()):
>>        varlist = spss.GetVariableName(var)
>>        if var <> "keyvar":
>>                spss.Submit("rename variables (%s = quest1_%s)." %
>> (varlist, varlist))
>> end program.
>>
>>
>> --- On Fri, 8/8/08, Lorenzo Sandini <[hidden email]> wrote:
>>
>>
>> From: Lorenzo Sandini <[hidden email]>
>>> Subject: Re: AW: rename all variables in a dataset
>>> To: [hidden email]
>>> Date: Friday, August 8, 2008, 6:45 AM
>>> Thanks Mario,
>>>
>>> Works perfectly. My datasets are *almost* identical in
>>> structure, so
>>> that most variables are present in all datasets, but some
>>> variables are
>>> specific to some of the datasets. For example
>>> questionnaires at 12
>>> months, 24 months, 36 months etc.. contain substantially
>>> more questions
>>> than 3 months, 6 months, 18 months, etc...,and some
>>> questions were only
>>> asked at baseline or at the 5yr closing visit.
>>>
>>> Is there a way to just rename ALL variables in a dataset
>>> without listing
>>> them ? (not that producing a list is complicated, but...).
>>> Of course the
>>> key variable will have to be returned to its original
>>> state. If not, no
>>> worries, this already did the job very well for me.
>>>
>>> Thanks again,
>>> Lorenzo
>>>
>>>
>>> Mario Giesel wrote:
>>>
>>> DEFINE !addsuffix (suffix = !TOKENS(1) / var =
>>>>
>>>> !CMDEND)
>>>
>>> !DO !i !IN (!var)
>>>> RENAME VAR (!i = !CONCAT(!i,!UNQUOTE(!suffix))).
>>>> !DOEND
>>>> !ENDDEFINE.
>>>> SET MPRINT = ON.
>>>> NUMERIC old1 old2 old3 (F1).
>>>> !addsuffix suffix = new var = old1 old2 old3.
>>>> SET MPRINT = OFF
>>>> Good luck,
>>>> Mario
>>>>
>>>>
>>>>
>>>> ----- Ursprüngliche Mail ----
>>>> Von: Lorenzo Sandini
>>>>
>>>> <[hidden email]>
>>>
>>> An: [hidden email]
>>>> Gesendet: Donnerstag, den 7. August 2008, 19:02:21 Uhr
>>>> Betreff: rename all variables in a dataset
>>>>
>>>> Hello,
>>>>
>>>> I a new at this so be patient please  :)
>>>>
>>>> I need to rename all the variables in a couple of
>>>>
>>>> datasets by adding a
>>>
>>> short suffix to them. I was told in the
>>>>
>>>> comp.soft-sys.stat.spss
>>>
>>> newsgroup that I could do it with a macro, but I am
>>>>
>>>> too new at this to
>>>
>>> do it by myself.
>>>>
>>>> Bruce Weaver proposed an elegant solution of stacking
>>>>
>>>> datasets, but I am
>>>
>>> afraid  this would make things mode difficult for me
>>>>
>>>> later, as I have to
>>>
>>> share this data with people  who know as little as me
>>>>
>>>> about SPSS. I
>>>
>>> paste our  discussion below for reference.
>>>>
>>>> From what I have read on R. Levesque's SPSS
>>>>
>>>> website, macros are really
>>>
>>> powerful ways of manipulating data without writing
>>>>
>>>> novel-long syntaxes.
>>>
>>> Still, I haven't found a way to do this. Could
>>>>
>>>> anyone offer me a scratch
>>>
>>> in the back ?
>>>>
>>>> Thanks !
>>>>
>>>> Lorenzo
>>>>
>>>>
>>>> QUOTE:
>>>> =====================================
>>>> Lorenzo Sandini wrote:
>>>>
>>>>
>>>> Hello,
>>>>>
>>>>> Can anyone tell me how to rename all variables in
>>>>>
>>>>> a set, for example
>>>
>>>  appending a suffix at the end of every variable ?
>>>>>
>>>>> The same survey has been repeatedly filled by
>>>>>
>>>>> participants at regular
>>>
>>>  intervals, and the answers to the repeatd
>>>>>
>>>>> questions have been saved
>>>
>>>  into various tables of an Access database, with an
>>>>>
>>>>> identical name
>>>
>>>  (question1, question2, question3, ....)
>>>>>
>>>>> I imported the various tables as separate datasets
>>>>>
>>>>> (questionnaire 3mo,
>>>
>>>  6mo, 9mo, etc...) and they all have the same
>>>>>
>>>>> variables, that I would
>>>
>>>  like to join into one dataset with variables like
>>>>>
>>>>> :
>>>
>>>  question1_3mo, question1_6mo, question1_9mo,
>>>>> question2_3mo, question2_6mo, question2_9mo,
>>>>>
>>>>> etc...
>>>
>>>  This could be done by hand since the number of
>>>>>
>>>>> variable is reasonable,
>>>
>>>  but I'd be happy to learn something new and
>>>>>
>>>>> apply it later in bigger
>>>
>>>  datasets.
>>>>>
>>>>> Thanks for your help !
>>>>>
>>>>> Lorenzo
>>>>>
>>>>>
>>>>> No doubt you could write a macro to do this.  But it
>>>>
>>>> might be simpler to
>>>
>>> use ADD FILES to stack your individual data sets, then
>>>>
>>>> CASESTOVARS to
>>>
>>> restructure from the long to wide format.  When
>>>>
>>>> stacking the files, use
>>>
>>> the /IN subcommand to create flags for the different
>>>>
>>>> time points, and
>>>
>>> then after merging all the files, compute an index
>>>>
>>>> variable to be used
>>>
>>> by CASESTOVARS.  I don't have SPSS on this
>>>>
>>>> machine, so cannot generate
>>>
>>> an example, but I think something along these lines
>>>>
>>>> should work.
>>>
>>> ADD FILES
>>>> file = "C:\MyFolder\data3mo.sav" /
>>>>
>>>> in = flag3mo /
>>>
>>> file = "C:\MyFolder\data6mo.sav" /
>>>>
>>>> in = flag6mo /
>>>
>>> file = "C:\MyFolder\data9mo.sav" /
>>>>
>>>> in = flag9mo .
>>>
>>> EXE.
>>>>
>>>> do if flag3mo.
>>>> -  compute time = 3 .
>>>> else if flag6mo
>>>> -  compute time = 6 ..
>>>> else.
>>>> -  compute time = 9 .
>>>> end if.
>>>> exe.
>>>>
>>>> sort cases by id time.
>>>> CASESTOVARS
>>>> id=idvar /
>>>> index = time /
>>>> drop {list of unneeded variables }.
>>>>
>>>>
>>>> --
>>>> Bruce Weaver
>>>> [hidden email]
>>>> www.angelfire.com/wv/bwhomedir
>>>> "When all else fails, RTFM."
>>>> ===============================
>>>>
>>>> =====================
>>>> 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
>>>>
>>>>
>>>> __________________________________________________
>>>> Do You Yahoo!?
>>>> Sie sind Spam leid? Yahoo! Mail verfügt über einen
>>>>
>>>> herausragenden Schutz gegen Massenmails.
>>>
>>> http://mail.yahoo.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
>>>
>>>
>> =====================
>> 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
>

====================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