Renaming Variables in a Macro

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

Renaming Variables in a Macro

Atai Winkler
Hi

I would like to rename a number of variables using a macro. The variables
are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new names
will be as the current names but without the suffix _1, i.e.  no_failures,
no_exposed_to_risk AND prop_failed.

I have written the following macro but it fails on the RENAME VARIABLES line
and I do not understand why.

DEFINE !rename_vars (var_list = !CHAREND('.'))

!DO !i !IN (!var_list)
COMPUTE length = !LENGTH(!i).
COMPUTE length_1 = length - 2.
RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
!DOEND
EXECUTE.

DELETE VARIABLES length  length_1.

!ENDDEFINE.

!rename_vars    var_list=no_failures_1  no_exposed_to_risk_1  prop_failed_1.


Can anyone please explain where the error is? Thank you.

Atai Winkler






--
Sent from: http://spssx-discussion.1045642.n5.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
Reply | Threaded
Open this post in threaded view
|

Re: Renaming Variables in a Macro

David Marso
Administrator
Can't do it that way.
Here is a way...UNTESTED


DEFINE !rename_vars (var_list = !CHAREND('.'))

!DO !i !IN (!var_list)
RENAME VARIABLES  (!i = !SUBSTR(!i, 1,
!LENGTH(!SUBSTR(!BLANKS(!LENGTH(!i)),3)) ).
!DOEND

!ENDDEFINE.

!rename_vars    var_list=no_failures_1  no_exposed_to_risk_1  prop_failed_1.





Atai Winkler wrote

> Hi
>
> I would like to rename a number of variables using a macro. The variables
> are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new names
> will be as the current names but without the suffix _1, i.e.  no_failures,
> no_exposed_to_risk AND prop_failed.
>
> I have written the following macro but it fails on the RENAME VARIABLES
> line
> and I do not understand why.
>
> DEFINE !rename_vars (var_list = !CHAREND('.'))
>
> !DO !i !IN (!var_list)
> COMPUTE length = !LENGTH(!i).
> COMPUTE length_1 = length - 2.
> RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
> !DOEND
> EXECUTE.
>
> DELETE VARIABLES length  length_1.
>
> !ENDDEFINE.
>
> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
> prop_failed_1.
>
>
> Can anyone please explain where the error is? Thank you.
>
> Atai Winkler
>
>
>
>
>
>
> --
> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Renaming Variables in a Macro

Atai Winkler
Hi David

Thank you for the reply. Your solution worked but I have two questions.

1. According to https://www.ibm.com/support/knowledgecenter/en/SSLVMB_24.0.0/spss/base/syn_define_string_manipulation_functions.html

!BLANKS has one argument but your solution has two arguments.

2. What is wrong with my (non-)solution?

Thank you.

Atai



-----Original Message-----
From: SPSSX(r) Discussion <[hidden email]> On Behalf Of David Marso
Sent: Tuesday, 1 May, 2018 4:21 PM
To: [hidden email]
Subject: Re: Renaming Variables in a Macro

Can't do it that way.
Here is a way...UNTESTED


DEFINE !rename_vars (var_list = !CHAREND('.'))

!DO !i !IN (!var_list)
RENAME VARIABLES  (!i = !SUBSTR(!i, 1,
!LENGTH(!SUBSTR(!BLANKS(!LENGTH(!i)),3)) ).
!DOEND

!ENDDEFINE.

!rename_vars    var_list=no_failures_1  no_exposed_to_risk_1  prop_failed_1.





Atai Winkler wrote

> Hi
>
> I would like to rename a number of variables using a macro. The
> variables
> are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new
> names will be as the current names but without the suffix _1, i.e.  
> no_failures, no_exposed_to_risk AND prop_failed.
>
> I have written the following macro but it fails on the RENAME
> VARIABLES line and I do not understand why.
>
> DEFINE !rename_vars (var_list = !CHAREND('.'))
>
> !DO !i !IN (!var_list)
> COMPUTE length = !LENGTH(!i).
> COMPUTE length_1 = length - 2.
> RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
> !DOEND
> EXECUTE.
>
> DELETE VARIABLES length  length_1.
>
> !ENDDEFINE.
>
> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
> prop_failed_1.
>
>
> Can anyone please explain where the error is? Thank you.
>
> Atai Winkler
>
>
>
>
>
>
> --
> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.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: Renaming Variables in a Macro

David Marso
Administrator
Atai Winkler wrote

> 1. My use of !BLANKS has a single argument !LENGTH(!i) ie
> !BLANKS(!LENGTH(!i))
> !SUBSTR uses the characters 3 and beyond to yield !LENGTH(!i) MINUS 2
> MACRO is basically brain dead as far as math is concerned so we need to be
> tricksters.
>
> 2. Macro has NO IDEA what the value of length_1 is.
>
> HTH
>
> Hi David
>
> Thank you for the reply. Your solution worked but I have two questions.
>
> 1. According to
> https://www.ibm.com/support/knowledgecenter/en/SSLVMB_24.0.0/spss/base/syn_define_string_manipulation_functions.html
>
> !BLANKS has one argument but your solution has two arguments.
>
> 2. What is wrong with my (non-)solution?
>
> Thank you.
>
> Atai
>
>
>
> -----Original Message-----
> From: SPSSX(r) Discussion &lt;

> SPSSX-L@.UGA

> &gt; On Behalf Of David Marso
> Sent: Tuesday, 1 May, 2018 4:21 PM
> To:

> SPSSX-L@.UGA

> Subject: Re: Renaming Variables in a Macro
>
> Can't do it that way.
> Here is a way...UNTESTED
>
>
> DEFINE !rename_vars (var_list = !CHAREND('.'))
>
> !DO !i !IN (!var_list)
> RENAME VARIABLES  (!i = !SUBSTR(!i, 1,
> !LENGTH(!SUBSTR(!BLANKS(!LENGTH(!i)),3)) ).
> !DOEND
>
> !ENDDEFINE.
>
> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
> prop_failed_1.
>
>
>
>
>
> Atai Winkler wrote
>> Hi
>>
>> I would like to rename a number of variables using a macro. The
>> variables
>> are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new
>> names will be as the current names but without the suffix _1, i.e.  
>> no_failures, no_exposed_to_risk AND prop_failed.
>>
>> I have written the following macro but it fails on the RENAME
>> VARIABLES line and I do not understand why.
>>
>> DEFINE !rename_vars (var_list = !CHAREND('.'))
>>
>> !DO !i !IN (!var_list)
>> COMPUTE length = !LENGTH(!i).
>> COMPUTE length_1 = length - 2.
>> RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
>> !DOEND
>> EXECUTE.
>>
>> DELETE VARIABLES length  length_1.
>>
>> !ENDDEFINE.
>>
>> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
>> prop_failed_1.
>>
>>
>> Can anyone please explain where the error is? Thank you.
>>
>> Atai Winkler
>>
>>
>>
>>
>>
>>
>> --
>> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>
>> LISTSERV@.UGA
>
>>  (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
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
> --
> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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@.UGA

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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Renaming Variables in a Macro

Art Kendall
In reply to this post by Atai Winkler
Why do you need a macro rather than just syntax?

something like:
open a new syntax window.
copy or type a list of the original variables into the window.
go back to the original syntax window and past the old list.
go back to the new syntax. Find and replace the suffix  to produce a new
list.
past that back in the old syntax window.
edit in the rest of a RENAME VARIABLES command.





-----
Art Kendall
Social Research Consultants
--
Sent from: http://spssx-discussion.1045642.n5.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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Renaming Variables in a Macro

Atai Winkler
If I were renaming the variables once, I would not use a macro but since I need to carry out similar renaming about twenty times (with suffices _2, _3, etc.), a macro is a better solution.



-----Original Message-----
From: SPSSX(r) Discussion <[hidden email]> On Behalf Of Art Kendall
Sent: Tuesday, 1 May, 2018 6:42 PM
To: [hidden email]
Subject: Re: Renaming Variables in a Macro

Why do you need a macro rather than just syntax?

something like:
open a new syntax window.
copy or type a list of the original variables into the window.
go back to the original syntax window and past the old list.
go back to the new syntax. Find and replace the suffix  to produce a new list.
past that back in the old syntax window.
edit in the rest of a RENAME VARIABLES command.





-----
Art Kendall
Social Research Consultants
--
Sent from: http://spssx-discussion.1045642.n5.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: Renaming Variables in a Macro

Jon Peck
In reply to this post by Atai Winkler
To expand on David' solution and the problem you ran into, when you wrote
COMPUTE length = !LENGTH(!i).
COMPUTE length_1 = length - 2.
RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
you compute casewise variables length and length_1, so it doesn't make sense to use those in the RENAME VARIABLES syntax.  And, anyway, data values cannot be used in a macro.  The entire macro is expanded before any syntax is executed, so even if length_1 were a scalar, its value wouldn't be known when the syntax is prepared for execution.  That sort of interaction in general requires Python programming.

As a further point, suppose you wanted to rename all variables whose names ended in _n, where n is a digit.  Rather than  having to list them all explicitly, which is tedious and error prone, you could use the SPSSINC SELECT VARIABLES to define a macro listing those and then use it in the renaming macro.

E.g.,
SPSSINC SELECT VARIABLES MACRONAME="!numericEnd" 
/PROPERTIES  PATTERN = ".*_\d$".

!rename_vars var_list=!numericEnd.




On Tue, May 1, 2018 at 10:09 AM, Atai Winkler <[hidden email]> wrote:
Hi David

Thank you for the reply. Your solution worked but I have two questions.

1.      According to https://www.ibm.com/support/knowledgecenter/en/SSLVMB_24.0.0/spss/base/syn_define_string_manipulation_functions.html

!BLANKS has one argument but your solution has two arguments.

2.      What is wrong with my (non-)solution?

Thank you.

Atai



-----Original Message-----
From: SPSSX(r) Discussion <[hidden email]> On Behalf Of David Marso
Sent: Tuesday, 1 May, 2018 4:21 PM
To: [hidden email]
Subject: Re: Renaming Variables in a Macro

Can't do it that way.
Here is a way...UNTESTED


DEFINE !rename_vars (var_list = !CHAREND('.'))

!DO !i !IN (!var_list)
RENAME VARIABLES  (!i = !SUBSTR(!i, 1,
!LENGTH(!SUBSTR(!BLANKS(!LENGTH(!i)),3)) ).
!DOEND

!ENDDEFINE.

!rename_vars    var_list=no_failures_1  no_exposed_to_risk_1  prop_failed_1.





Atai Winkler wrote
> Hi
>
> I would like to rename a number of variables using a macro. The
> variables
> are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new
> names will be as the current names but without the suffix _1, i.e. 
> no_failures, no_exposed_to_risk AND prop_failed.
>
> I have written the following macro but it fails on the RENAME
> VARIABLES line and I do not understand why.
>
> DEFINE !rename_vars (var_list = !CHAREND('.'))
>
> !DO !i !IN (!var_list)
> COMPUTE length = !LENGTH(!i).
> COMPUTE length_1 = length - 2.
> RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
> !DOEND
> EXECUTE.
>
> DELETE VARIABLES length  length_1.
>
> !ENDDEFINE.
>
> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
> prop_failed_1.
>
>
> Can anyone please explain where the error is? Thank you.
>
> Atai Winkler
>
>
>
>
>
>
> --
> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.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



--
Jon K Peck
[hidden email]

===================== 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 Variables in a Macro

Jon Peck
In reply to this post by David Marso
That only works if the number after _ in the name is a single digit, which may be okay in this case but could trigger misbehavior out in the wild.

On Tue, May 1, 2018 at 11:31 AM, David Marso <[hidden email]> wrote:
Atai Winkler wrote
> 1. My use of !BLANKS has a single argument !LENGTH(!i) ie
> !BLANKS(!LENGTH(!i))
> !SUBSTR uses the characters 3 and beyond to yield !LENGTH(!i) MINUS 2
> MACRO is basically brain dead as far as math is concerned so we need to be
> tricksters.
>
> 2. Macro has NO IDEA what the value of length_1 is.
>
> HTH
>
> Hi David
>
> Thank you for the reply. Your solution worked but I have two questions.
>
> 1.    According to
> https://www.ibm.com/support/knowledgecenter/en/SSLVMB_24.0.0/spss/base/syn_define_string_manipulation_functions.html
>
> !BLANKS has one argument but your solution has two arguments.
>
> 2.    What is wrong with my (non-)solution?
>
> Thank you.
>
> Atai
>
>
>
> -----Original Message-----
> From: SPSSX(r) Discussion &lt;

> SPSSX-L@.UGA

> &gt; On Behalf Of David Marso
> Sent: Tuesday, 1 May, 2018 4:21 PM
> To:

> SPSSX-L@.UGA

> Subject: Re: Renaming Variables in a Macro
>
> Can't do it that way.
> Here is a way...UNTESTED
>
>
> DEFINE !rename_vars (var_list = !CHAREND('.'))
>
> !DO !i !IN (!var_list)
> RENAME VARIABLES  (!i = !SUBSTR(!i, 1,
> !LENGTH(!SUBSTR(!BLANKS(!LENGTH(!i)),3)) ).
> !DOEND
>
> !ENDDEFINE.
>
> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
> prop_failed_1.
>
>
>
>
>
> Atai Winkler wrote
>> Hi
>>
>> I would like to rename a number of variables using a macro. The
>> variables
>> are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new
>> names will be as the current names but without the suffix _1, i.e. 
>> no_failures, no_exposed_to_risk AND prop_failed.
>>
>> I have written the following macro but it fails on the RENAME
>> VARIABLES line and I do not understand why.
>>
>> DEFINE !rename_vars (var_list = !CHAREND('.'))
>>
>> !DO !i !IN (!var_list)
>> COMPUTE length = !LENGTH(!i).
>> COMPUTE length_1 = length - 2.
>> RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
>> !DOEND
>> EXECUTE.
>>
>> DELETE VARIABLES length  length_1.
>>
>> !ENDDEFINE.
>>
>> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
>> prop_failed_1.
>>
>>
>> Can anyone please explain where the error is? Thank you.
>>
>> Atai Winkler
>>
>>
>>
>>
>>
>>
>> --
>> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>
>> LISTSERV@.UGA
>
>>  (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
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
> --
> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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@.UGA

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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.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



--
Jon K Peck
[hidden email]

===================== 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 Variables in a Macro

David Marso
Administrator
In that case use !INDEX to locate the _ and chop one off ...bleeding eyeballs
may ensue.

RENAME VARIABLES  (!i = !SUBSTR(!i,
1,!LENGTH(!SUBSTR(!BLANKS(!INDEX(!i,"_")) ,2) )) ).
---


Jon Peck wrote
> That only works if the number after _ in the name is a single digit, which
> may be okay in this case but could trigger misbehavior out in the wild.
>
> On Tue, May 1, 2018 at 11:31 AM, David Marso &lt;

> david.marso@

> &gt; wrote:
>
>> Atai Winkler wrote
>> > 1. My use of !BLANKS has a single argument !LENGTH(!i) ie
>> > !BLANKS(!LENGTH(!i))
>> > !SUBSTR uses the characters 3 and beyond to yield !LENGTH(!i) MINUS 2
>> > MACRO is basically brain dead as far as math is concerned so we need to
>> be
>> > tricksters.
>> >
>> > 2. Macro has NO IDEA what the value of length_1 is.
>> >
>> > HTH
>> >
>> > Hi David
>> >
>> > Thank you for the reply. Your solution worked but I have two questions.
>> >
>> > 1.    According to
>> > https://www.ibm.com/support/knowledgecenter/en/SSLVMB_24.
>> 0.0/spss/base/syn_define_string_manipulation_functions.html
>> >
>> > !BLANKS has one argument but your solution has two arguments.
>> >
>> > 2.    What is wrong with my (non-)solution?
>> >
>> > Thank you.
>> >
>> > Atai
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: SPSSX(r) Discussion &lt;
>>
>> > SPSSX-L@.UGA
>>
>> > &gt; On Behalf Of David Marso
>> > Sent: Tuesday, 1 May, 2018 4:21 PM
>> > To:
>>
>> > SPSSX-L@.UGA
>>
>> > Subject: Re: Renaming Variables in a Macro
>> >
>> > Can't do it that way.
>> > Here is a way...UNTESTED
>> >
>> >
>> > DEFINE !rename_vars (var_list = !CHAREND('.'))
>> >
>> > !DO !i !IN (!var_list)
>> > RENAME VARIABLES  (!i = !SUBSTR(!i, 1,
>> > !LENGTH(!SUBSTR(!BLANKS(!LENGTH(!i)),3)) ).
>> > !DOEND
>> >
>> > !ENDDEFINE.
>> >
>> > !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
>> > prop_failed_1.
>> >
>> >
>> >
>> >
>> >
>> > Atai Winkler wrote
>> >> Hi
>> >>
>> >> I would like to rename a number of variables using a macro. The
>> >> variables
>> >> are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new
>> >> names will be as the current names but without the suffix _1, i.e.
>> >> no_failures, no_exposed_to_risk AND prop_failed.
>> >>
>> >> I have written the following macro but it fails on the RENAME
>> >> VARIABLES line and I do not understand why.
>> >>
>> >> DEFINE !rename_vars (var_list = !CHAREND('.'))
>> >>
>> >> !DO !i !IN (!var_list)
>> >> COMPUTE length = !LENGTH(!i).
>> >> COMPUTE length_1 = length - 2.
>> >> RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
>> >> !DOEND
>> >> EXECUTE.
>> >>
>> >> DELETE VARIABLES length  length_1.
>> >>
>> >> !ENDDEFINE.
>> >>
>> >> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
>> >> prop_failed_1.
>> >>
>> >>
>> >> Can anyone please explain where the error is? Thank you.
>> >>
>> >> Atai Winkler
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> --
>> >> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>> >>
>> >> =====================
>> >> To manage your subscription to SPSSX-L, send a message to
>> >
>> >> LISTSERV@.UGA
>> >
>> >>  (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
>> >
>> >
>> >
>> >
>> >
>> > -----
>> > Please reply to the list and not to my personal email.
>> > Those desiring my consulting or training services please feel free to
>> > email me.
>> > ---
>> > "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> > ne forte conculcent eas pedibus suis."
>> > Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> > abyssum?"
>> > --
>> > Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>> >
>> > =====================
>> > To manage your subscription to SPSSX-L, send a message to
>>
>> > LISTSERV@.UGA
>>
>> >  (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@.UGA
>>
>> >  (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
>>
>>
>>
>>
>>
>> -----
>> Please reply to the list and not to my personal email.
>> Those desiring my consulting or training services please feel free to
>> email me.
>> ---
>> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> ne forte conculcent eas pedibus suis."
>> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> abyssum?"
>> --
>> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (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
>>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Renaming Variables in a Macro

Jon Peck
To be  bulletproof, I would want to handle mixtures of 1, 2, or more trailing digits and to accommodate variable names that have nonterminal _ in them or end with _.  

Here's a solution.  It illustrates the use of Python functions requiring arguments without the need for the user of the function to write Python syntax once the function is defined.  Once defined, the function is called with its arguments using the  SPSSINC PROGRAM extension command, which is available via Extensions > Extension Hub or in older versions the Utiities menu if it is not already installed.

First, the function to do the conditional renaming is defined.
begin program.
import spss, sys, re

def renames():
    for v in sys.argv:
        v2 = re.sub(r"_\d+$", "", v)
        if v2 != v:
            spss.Submit("rename variables %s = %s" % (v, v2))
end program.

This code creates a function named renames that loops over the listed variables (which could come from the SPSSINC SELECT VARIABLES extension command that I mentioned yesterday).  If it finds a name matching the pattern, i.e., ending with an _ followed by one or more digits, it constructs the root name and runs a RENAME VARIABLES command.

Then code like this can be used to do the renaming.

SPSSINC PROGRAM renames educ_99 job_cat_1 jobtime.

It produces variables named educ, job_cat, and leaves jobtime alone.  Of course, the new names must not already be in use in the dataset.

On Wed, May 2, 2018 at 12:03 AM, David Marso <[hidden email]> wrote:
In that case use !INDEX to locate the _ and chop one off ...bleeding eyeballs
may ensue.

RENAME VARIABLES  (!i = !SUBSTR(!i,
1,!LENGTH(!SUBSTR(!BLANKS(!INDEX(!i,"_")) ,2) )) ).
---


Jon Peck wrote
> That only works if the number after _ in the name is a single digit, which
> may be okay in this case but could trigger misbehavior out in the wild.
>
> On Tue, May 1, 2018 at 11:31 AM, David Marso &lt;

> david.marso@

> &gt; wrote:
>
>> Atai Winkler wrote
>> > 1. My use of !BLANKS has a single argument !LENGTH(!i) ie
>> > !BLANKS(!LENGTH(!i))
>> > !SUBSTR uses the characters 3 and beyond to yield !LENGTH(!i) MINUS 2
>> > MACRO is basically brain dead as far as math is concerned so we need to
>> be
>> > tricksters.
>> >
>> > 2. Macro has NO IDEA what the value of length_1 is.
>> >
>> > HTH
>> >
>> > Hi David
>> >
>> > Thank you for the reply. Your solution worked but I have two questions.
>> >
>> > 1.    According to
>> > https://www.ibm.com/support/knowledgecenter/en/SSLVMB_24.
>> 0.0/spss/base/syn_define_string_manipulation_functions.html
>> >
>> > !BLANKS has one argument but your solution has two arguments.
>> >
>> > 2.    What is wrong with my (non-)solution?
>> >
>> > Thank you.
>> >
>> > Atai
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: SPSSX(r) Discussion &lt;
>>
>> > SPSSX-L@.UGA
>>
>> > &gt; On Behalf Of David Marso
>> > Sent: Tuesday, 1 May, 2018 4:21 PM
>> > To:
>>
>> > SPSSX-L@.UGA
>>
>> > Subject: Re: Renaming Variables in a Macro
>> >
>> > Can't do it that way.
>> > Here is a way...UNTESTED
>> >
>> >
>> > DEFINE !rename_vars (var_list = !CHAREND('.'))
>> >
>> > !DO !i !IN (!var_list)
>> > RENAME VARIABLES  (!i = !SUBSTR(!i, 1,
>> > !LENGTH(!SUBSTR(!BLANKS(!LENGTH(!i)),3)) ).
>> > !DOEND
>> >
>> > !ENDDEFINE.
>> >
>> > !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
>> > prop_failed_1.
>> >
>> >
>> >
>> >
>> >
>> > Atai Winkler wrote
>> >> Hi
>> >>
>> >> I would like to rename a number of variables using a macro. The
>> >> variables
>> >> are: no_failures_1,  no_exposed_to_risk_1 AND prop_failed_1. The new
>> >> names will be as the current names but without the suffix _1, i.e.
>> >> no_failures, no_exposed_to_risk AND prop_failed.
>> >>
>> >> I have written the following macro but it fails on the RENAME
>> >> VARIABLES line and I do not understand why.
>> >>
>> >> DEFINE !rename_vars (var_list = !CHAREND('.'))
>> >>
>> >> !DO !i !IN (!var_list)
>> >> COMPUTE length = !LENGTH(!i).
>> >> COMPUTE length_1 = length - 2.
>> >> RENAME VARIABLES (!i = !SUBSTR(!i, 1, length_1)).
>> >> !DOEND
>> >> EXECUTE.
>> >>
>> >> DELETE VARIABLES length  length_1.
>> >>
>> >> !ENDDEFINE.
>> >>
>> >> !rename_vars    var_list=no_failures_1  no_exposed_to_risk_1
>> >> prop_failed_1.
>> >>
>> >>
>> >> Can anyone please explain where the error is? Thank you.
>> >>
>> >> Atai Winkler
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> --
>> >> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>> >>
>> >> =====================
>> >> To manage your subscription to SPSSX-L, send a message to
>> >
>> >> LISTSERV@.UGA
>> >
>> >>  (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
>> >
>> >
>> >
>> >
>> >
>> > -----
>> > Please reply to the list and not to my personal email.
>> > Those desiring my consulting or training services please feel free to
>> > email me.
>> > ---
>> > "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> > ne forte conculcent eas pedibus suis."
>> > Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> > abyssum?"
>> > --
>> > Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>> >
>> > =====================
>> > To manage your subscription to SPSSX-L, send a message to
>>
>> > LISTSERV@.UGA
>>
>> >  (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@.UGA
>>
>> >  (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
>>
>>
>>
>>
>>
>> -----
>> Please reply to the list and not to my personal email.
>> Those desiring my consulting or training services please feel free to
>> email me.
>> ---
>> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> ne forte conculcent eas pedibus suis."
>> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> abyssum?"
>> --
>> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (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
>>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
Sent from: http://spssx-discussion.1045642.n5.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



--
Jon K Peck
[hidden email]

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