Date: Mon, 26 Jan 2009 15:53:18 -0600
Reply-To: "Peck, Jon" <peck@spss.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Peck, Jon" <peck@spss.com>
Subject: Re: delete string variables
In-Reply-To: A<938008.11105.qm@web110714.mail.gq1.yahoo.com>
Content-Type: text/plain; charset="US-ASCII"
Well one pretty easy way if you have the Data Validation option would be to use that to construct a table of empty variables, capture that table with OMS, and use a trivial Python program to generate a delete variables command listing them.
It's not two commands, but it would be fewer than ten.
Even shorter would be to use the function FindEmptyVars in the spssaux2.py module on Developer Central.
begin program.
import spss, spssaux2
FindEmptyVars(delete=True)
end program.
HTH,
Jon Peck
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of Albert-jan Roskam
Sent: Monday, January 26, 2009 11:40 AM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Re: [SPSSX-L] delete string variables
Hi!
The code below works, but it's not very concise. Maybe Jon Peck or somebody else has an ingenious two-line solution ;-)
v1, v4, blankstr and fubar are the vars to be deleted.
Cheers!!
Albert-Jan
* create a sample dataset: .
data list free / v1 to v4.
begin data
0 0 1 0
0 1 0 0
0 0 0 0
0 1 0 0
0 0 0 0
0 1 1 0
0 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
end data.
recode all (0 = sysmis).
string blankstr fubar (a8).
compute blankstr = " " .
compute fubar = " " .
exe.
* actual code.
begin program.
import spss, spssaux
spss.Submit("""
compute const = 1.
exe.
dataset name source.""")
# check numeric vars
numDict = spssaux.VariableDict(variableType='numeric')
cmd = ["aggr out = * / break = const / "] + [" " + var.VariableName + " = sum (" + var.VariableName + ") / " \
for var in numDict if var.VariableName != "const"] + ["."]
spss.Submit(cmd)
spss.Submit("""
dataset name thisone.
dataset activate source.""")
# check string vars
strDict = spssaux.VariableDict(variableType='string')
for var in strDict:
spss.Submit("compute len_%s = length(rtrim(ltrim( %s )))." % (var.VariableName, var.VariableName))
cmd = ["aggr out = * / break = const / "] + [" " + var.VariableName + " = sum (len_" + var.VariableName + ") / " \
for var in strDict if var.VariableName != "const"] + ["."]
spss.Submit(cmd)
# build list of vars to be deleted
spss.Submit("""
dataset name thatone window = asis.
match files / file = thisone / file = thatone / by = const.
recode all (sysmis = 0) (else = copy).
exe.""")
dataCursor=spss.Cursor()
oneRow=dataCursor.fetchone()
varDict = spssaux.VariableDict()
delvars = [str(var) for k, var in enumerate(varDict) if oneRow[k] == 0]
dataCursor.close()
spss.Submit("dataset activate source.")
lastvar = ' '.join(spssaux.GetVariableNamesList()[-1:])
spss.Submit("""
add files / file = * / drop = %s const to %s.
exe.
dataset close all.
""" % (' '.join(delvars), lastvar))
end program.
=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (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@LISTSERV.UGA.EDU (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