Any command

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

Any command

Mike P-5
Hi All,

Going back to a something I posted a while ago, I have a dataset that I
import and want to create a new variable based on what is in the
variable label

In the situation below, I want to create a variable called sky, where if
anywhere in the variable name the word 'sky' appears, and has a value of
1.
In the example dataset this would mean running the following code

compute sky = any(1,banner_sky,sky_another).

I'm open to other suggestions not including python, but I think the
solution with python will be the best and most elegant.

The problem I get at the minute with the python code is it is not
creating the list of variables to put into the any command

* Sample dataset *.
DATA LIST list /id(F2.0) banner_sky(F1.0) overlay_200x600(F1.0)
a_600x_200_overlay(F1.0) sky_another(F1.0).
BEGIN DATA
1 0 0 1 0
2 1 0 0 1
3 1 0 1 1
4 0 0 0 1
5 0 0 1 0
6 0 1 1 1
7 1 1 0 1
8 1 1 1 0
9 1 1 0 1
10 0 1 0 0
END DATA.

begin program.
import spss, spssaux, re

vard = spssaux.VariableDict(pattern="sky")
spss_code = "compute sky = any(1, " + ",".join(vard.variables)+")"
print spss_code
##spss.Submit(spss_code)

end program.

Thanks in advance

Mike

______________________________________________________________________
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: Any command

Peck, Jon
You are close, but you need to change the expression for pattern to
".*sky"

The VariableDict class uses a match of the regular expression to the names.  That means a match starting from the first character of the name, so "sky" matches only names that start with sky.

When you write ".*sky", that means a sequence of zero or more arbitrary characters followed by sky.


HTH,
Jon Peck


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Michael Pearmain
Sent: Wednesday, December 06, 2006 5:44 AM
To: [hidden email]
Subject: [SPSSX-L] Any command

Hi All,

Going back to a something I posted a while ago, I have a dataset that I
import and want to create a new variable based on what is in the
variable label

In the situation below, I want to create a variable called sky, where if
anywhere in the variable name the word 'sky' appears, and has a value of
1.
In the example dataset this would mean running the following code

compute sky = any(1,banner_sky,sky_another).

I'm open to other suggestions not including python, but I think the
solution with python will be the best and most elegant.

The problem I get at the minute with the python code is it is not
creating the list of variables to put into the any command

* Sample dataset *.
DATA LIST list /id(F2.0) banner_sky(F1.0) overlay_200x600(F1.0)
a_600x_200_overlay(F1.0) sky_another(F1.0).
BEGIN DATA
1 0 0 1 0
2 1 0 0 1
3 1 0 1 1
4 0 0 0 1
5 0 0 1 0
6 0 1 1 1
7 1 1 0 1
8 1 1 1 0
9 1 1 0 1
10 0 1 0 0
END DATA.

begin program.
import spss, spssaux, re

vard = spssaux.VariableDict(pattern="sky")
spss_code = "compute sky = any(1, " + ",".join(vard.variables)+")"
print spss_code
##spss.Submit(spss_code)

end program.

Thanks in advance

Mike

______________________________________________________________________
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: Any command

Roberts, Michael
Hi,

Also, if you haven't already done it, remove the comment indicators
before the submit statement (##), else the code won't run.

Mike




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Peck, Jon
Sent: Wednesday, December 06, 2006 8:27 AM
To: [hidden email]
Subject: Re: Any command

You are close, but you need to change the expression for pattern to
".*sky"

The VariableDict class uses a match of the regular expression to the
names.  That means a match starting from the first character of the
name, so "sky" matches only names that start with sky.

When you write ".*sky", that means a sequence of zero or more arbitrary
characters followed by sky.


HTH,
Jon Peck


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Michael Pearmain
Sent: Wednesday, December 06, 2006 5:44 AM
To: [hidden email]
Subject: [SPSSX-L] Any command

Hi All,

Going back to a something I posted a while ago, I have a dataset that I
import and want to create a new variable based on what is in the
variable label

In the situation below, I want to create a variable called sky, where if
anywhere in the variable name the word 'sky' appears, and has a value of
1.
In the example dataset this would mean running the following code

compute sky = any(1,banner_sky,sky_another).

I'm open to other suggestions not including python, but I think the
solution with python will be the best and most elegant.

The problem I get at the minute with the python code is it is not
creating the list of variables to put into the any command

* Sample dataset *.
DATA LIST list /id(F2.0) banner_sky(F1.0) overlay_200x600(F1.0)
a_600x_200_overlay(F1.0) sky_another(F1.0).
BEGIN DATA
1 0 0 1 0
2 1 0 0 1
3 1 0 1 1
4 0 0 0 1
5 0 0 1 0
6 0 1 1 1
7 1 1 0 1
8 1 1 1 0
9 1 1 0 1
10 0 1 0 0
END DATA.

begin program.
import spss, spssaux, re

vard = spssaux.VariableDict(pattern="sky")
spss_code = "compute sky = any(1, " + ",".join(vard.variables)+")"
print spss_code
##spss.Submit(spss_code)

end program.

Thanks in advance

Mike

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