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
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.
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.