LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (January 2009)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 26 Jan 2009 10:39:37 -0800
Reply-To:     Albert-jan Roskam <fomcl@yahoo.com>
Sender:       "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:         Albert-jan Roskam <fomcl@yahoo.com>
Subject:      Re: delete string variables
Comments: To: Cristiano <rivoli2@gmail.com>
Content-Type: text/plain; charset=us-ascii

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


Back to: Top of message | Previous page | Main SPSSX-L page