Selection of variables by variable name

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

Selection of variables by variable name

Staffan Lindberg
Dear list!
 
I have a rather large material appr. 1000 variables. The variables are
divided into separate areas. The first letter of the variable name indicates
which area it belongs to. I sometimes wish to list the dictionary solely for
the variables belonging to a certain area (beginning with a certain letter).
In other cases I would like to save a subfile of all the variables belonging
to a certain area. The variables are not ordered by area but are randomly
scattered in the file. Is there a way to do this?
 
Staffan Lindberg
National Institute of Public Health
Sweden
Reply | Threaded
Open this post in threaded view
|

Re: Selection of variables by variable name

Peck, Jon
This is very easy to do if you have at least SPSS 14 with Python programmability installed and the spssaux module downloaded from SPSS Developer Central (www.spss.com/devcentral).

Without learning Python, you could use the following code, changing only the letter of the variable you want to select on.

BEGIN PROGRAM.
import spss, spssaux
vardict=spssaux.VariableDict(pattern="a")
spss.Submit("DISPLAY DICT VARIABLES=" + " ".join(vardict.variables))
END PROGRAM.

You would change "a" to whatever prefix you want to use to select variables.

Note that case matters in Python syntax (but not for the prefix) so the program should be entered exactly as shown here.

To create a subfile of the variables selected in this way, you could use the line
spss.Submit(r"SAVE OUTFILE='somefilename' /KEEP=" + " ".join(vardict.variables))
in the program where somefilename is the filename to write to.  Note that single quotes (') are used within the double quotes(") surrounding the entire command.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Staffan Lindberg
Sent: Friday, March 09, 2007 9:28 AM
To: [hidden email]
Subject: [SPSSX-L] Selection of variables by variable name

Dear list!

I have a rather large material appr. 1000 variables. The variables are
divided into separate areas. The first letter of the variable name indicates
which area it belongs to. I sometimes wish to list the dictionary solely for
the variables belonging to a certain area (beginning with a certain letter).
In other cases I would like to save a subfile of all the variables belonging
to a certain area. The variables are not ordered by area but are randomly
scattered in the file. Is there a way to do this?

Staffan Lindberg
National Institute of Public Health
Sweden
Reply | Threaded
Open this post in threaded view
|

Re: Selection of variables by variable name

Mike P-5
In reply to this post by Staffan Lindberg
If you have python installed you can use natural language

MaterialList = {'material':'first letter'}
for i in MaterialList.keys():
        vard = spssaux.VariableDict(pattern=".*%s" % MaterialList [i])
        jvars = string.join(vard.variables, ",")
        spss_sky = "compute %s = any(1, %s)" % (i, jvars)
        spss.Submit(spss_sky)

This will create a variable of all the materials that go into your file
from here you can choose to save / display dictionary do whatever you
like

I'm sure John Peck has a simpler way, but this would work

Mike

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Staffan Lindberg
Sent: 09 March 2007 15:28
To: [hidden email]
Subject: Selection of variables by variable name

Dear list!

I have a rather large material appr. 1000 variables. The variables are
divided into separate areas. The first letter of the variable name
indicates
which area it belongs to. I sometimes wish to list the dictionary solely
for
the variables belonging to a certain area (beginning with a certain
letter).
In other cases I would like to save a subfile of all the variables
belonging
to a certain area. The variables are not ordered by area but are
randomly
scattered in the file. Is there a way to do this?

Staffan Lindberg
National Institute of Public Health
Sweden

________________________________________________________________________
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
______________________________________________________________________