Variable labels and Python

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

Variable labels and Python

Mike P-5
Hi All,
I have a problem that I'm looking to solve via python. I want to search
the variable names for a sequence of characters (to generalise) and then
create a new variable based on there answers within the variable. e.g.
see below. does this make sense?

 Variable name:
overlayNewWomanROSv     ExpandableNewWomanROSv
ExpandableCNTravellerROSv
        1                    1                                0
       0                   1                                1
       0                   0                                0

i want to inspect the variable names and if it has 'Expandable' anywhere
in it I want to create a new variable called, lets say expandable.

In this situation the data entered would
EXPANDABLE
        1                        - from a one in new women ROS
        1                        - from CNT Traveller
        0                        - because neither has an entry.

I want to keep the variable dichotomous though.

I know I can do this in syntax but I thought there might be some kind of
shorter easier way using the spssaux module, guessing something like
below with regular expressions, but the below is wrong.

begin program.
import spss, spssaux, re

pat="^[Expandable]"
vard = spssaux.VariableDict(pattern=pat)
for v in vard:
        newname = re.sub(pat, "", str(v))
        spss.Submit("'IF (newname=1) expandable = 1 .
   EXECUTE .''')
end program.

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: Variable labels and Python

Peck, Jon
Apologies if you get two of these.  It appears that the first try failed.

You are on the right track with regular expressions and spssaux, but there are a few problems.
The regular expression "^[Expandable]" means to match any character in brackets if it is the first character of the expression.  So it would match E,x,p, etc.  You probably mean
"^Expandable" without the brackets.  The VariableDict pattern expression is not case sensitive, but the re.sub one is.  This can be changed, but I'll save that information until I hear that you need it.

The second problem is with the Submit.  You need the test condition to be a Python variable rather than a literal constant.  So, instead of  (no need for extra quote or execute)
       spss.Submit("IF (newname=1) expandable = 1")
write
       spss.Submit("IF (%s = 1) expandable = 1" % newname)

If you want the false case to result in expandable = 0 rather than sysmis, you should set it to zero before the loop.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion on behalf of Michael Pearmain
Sent: Thu 11/23/2006 6:51 AM
To: [hidden email]
Subject:      [SPSSX-L] Variable labels and Python
 
Hi All,
I have a problem that I'm looking to solve via python. I want to search
the variable names for a sequence of characters (to generalise) and then
create a new variable based on there answers within the variable. e.g.
see below. does this make sense?

 Variable name:
overlayNewWomanROSv     ExpandableNewWomanROSv
ExpandableCNTravellerROSv
        1                    1                                0
       0                   1                                1
       0                   0                                0

i want to inspect the variable names and if it has 'Expandable' anywhere
in it I want to create a new variable called, lets say expandable.

In this situation the data entered would
EXPANDABLE
        1                        - from a one in new women ROS
        1                        - from CNT Traveller
        0                        - because neither has an entry.

I want to keep the variable dichotomous though.

I know I can do this in syntax but I thought there might be some kind of
shorter easier way using the spssaux module, guessing something like
below with regular expressions, but the below is wrong.

begin program.
import spss, spssaux, re

pat="^[Expandable]"
vard = spssaux.VariableDict(pattern=pat)
for v in vard:
        newname = re.sub(pat, "", str(v))
        spss.Submit("'IF (newname=1) expandable = 1 .
   EXECUTE .''')
end program.

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: Variable labels and Python

Mike P-5
In reply to this post by Mike P-5
Hi Jon,

Thanks for the advice,

I'm trying to write on the fly as well as learn python so, I'm grabbing
bits of code that almost fit,  on closer inspection, I'm not wanting to
delete the word from the variable just create a new variable at the end
if it has a value greater than zero in any of the columns.

So I've changed slightly what I've written, as I don't care how many
instances there are.
I want to get zeros and ones as you said, but I can't seem to call the
function properly?

Any ideas?

Mike

begin program.
import spss, spssaux, re

def get_creatives(creative_type):
        vard = spssaux.VariableDict(pattern=creative_type)
        for v in vard.values:
                if v>0:
                        return True
        return False

if get_creative("Expandable"):
        spss.Submit('''Compute Exp =1.''')


end program.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Peck, Jon
Sent: 23 November 2006 15:47
To: [hidden email]
Subject: Re: Variable labels and Python

Apologies if you get two of these.  It appears that the first try
failed.

You are on the right track with regular expressions and spssaux, but
there are a few problems.
The regular expression "^[Expandable]" means to match any character in
brackets if it is the first character of the expression.  So it would
match E,x,p, etc.  You probably mean "^Expandable" without the brackets.
The VariableDict pattern expression is not case sensitive, but the
re.sub one is.  This can be changed, but I'll save that information
until I hear that you need it.

The second problem is with the Submit.  You need the test condition to
be a Python variable rather than a literal constant.  So, instead of
(no need for extra quote or execute)
       spss.Submit("IF (newname=1) expandable = 1") write
       spss.Submit("IF (%s = 1) expandable = 1" % newname)

If you want the false case to result in expandable = 0 rather than
sysmis, you should set it to zero before the loop.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion on behalf of Michael Pearmain
Sent: Thu 11/23/2006 6:51 AM
To: [hidden email]
Subject:      [SPSSX-L] Variable labels and Python

Hi All,
I have a problem that I'm looking to solve via python. I want to search
the variable names for a sequence of characters (to generalise) and then
create a new variable based on there answers within the variable. e.g.
see below. does this make sense?

 Variable name:
overlayNewWomanROSv     ExpandableNewWomanROSv
ExpandableCNTravellerROSv
        1                    1                                0
       0                   1                                1
       0                   0                                0

i want to inspect the variable names and if it has 'Expandable' anywhere
in it I want to create a new variable called, lets say expandable.

In this situation the data entered would EXPANDABLE
        1                        - from a one in new women ROS
        1                        - from CNT Traveller
        0                        - because neither has an entry.

I want to keep the variable dichotomous though.

I know I can do this in syntax but I thought there might be some kind of
shorter easier way using the spssaux module, guessing something like
below with regular expressions, but the below is wrong.

begin program.
import spss, spssaux, re

pat="^[Expandable]"
vard = spssaux.VariableDict(pattern=pat) for v in vard:
        newname = re.sub(pat, "", str(v))
        spss.Submit("'IF (newname=1) expandable = 1 .
   EXECUTE .''')
end program.

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. For more information on a proactive anti-virus
service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________