Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting variables based on variable name

1,817 views
Skip to first unread message

helic...@gmail.com

unread,
Feb 25, 2008, 2:37:17 AM2/25/08
to
Hi,

I've been trying to figure out if its possible to have SPSS delete
certain variables based on their name. These variables are working
variables that are no longer of use created by surveying software and
always follow the same naming conventions, an example of which is
Q3X01, Q3X02 .... Q3X25.

Obviously I could use the command 'Delete variables Q3X01 to Q3x25'
but the next survey might be Q6X01...Q6X12 and the syntax would need
to be adapted from one survey to the next, in which case its just
quicker to delete them manually.

So I'm looking for a way to delete these variables using a wildcard of
sorts, that is if the variables fit this pattern Q(NUMBER)X(NUMBER).

I tried the rather inelegant solution of creating a list of hundreds
of delete variable commands encompassing all possible unwanted
variables in future surveys but SPSS threw so many warnings back at me
that my system slowed to a crawl, and I've had no luck at trying to
turn these warnings off.

Any solution or workarounds would be greatly appreciated,

Thanks in advance.

H1

fred....@lsc.gov.uk

unread,
Feb 25, 2008, 3:32:30 AM2/25/08
to
The following macro will do what you want, altho you will get a
warning if the variable doesn't exist, but you can ignore them.

set mprints on.
DEFINE !delvar().


!DO !i=1 !to 9.
!DO !j=1 !TO 9.
delete variable !concat('Q',!i,'X0',!j).
!DOEND.
!DOEND.

!DO !i=1 !to 9.
!DO !j=10 !TO 99.
delete variable !concat('Q',!i,'X',!j).
!DOEND.
!DOEND.
!ENDDEFINE.

!delvar.

JKPeck

unread,
Feb 25, 2008, 9:32:32 AM2/25/08
to

If you can use programmability, the following will do the job. It
finds all variables of the form Q, one or more digits, X, one or more
digits, anything else.

Then it deletes all variables in that list.

HTH,
Jon Peck


begin program.
import spss, spssaux
vard = spssaux.VariableDict(pattern=r"Q\d+X\d+")
spss.Submit("DELETE VARIABLES " + " ".join(vard.variables))
end program.

helic...@gmail.com

unread,
Feb 26, 2008, 3:34:56 AM2/26/08
to

Thank you both for your suggestions - the macro would have worked had
there been fewer possible combinations but the sheer number of invalid
variable names generated causes SPSS to crash. The little python
script works like a charm. Thanks very much again.

agus...@gmail.com

unread,
Dec 17, 2012, 8:23:40 AM12/17/12
to
I'd really like to use this syntax (?) but I don't know how to executed. Thus, I'd greatly appreciate if you could help me.
Thank you in advance,
Agustin
0 new messages