SPSS syntax to delete string variables with width more than 500

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

SPSS syntax to delete string variables with width more than 500

Kanch
Hi I am new to SPSS and I am looking for a syntax to delete variables than have width more than 500.

I came across the syntax dict1 = spssaux.VariableDict(variableType='string'). I am not sure if it will be helpful. It'll be great if anyone could help me with this! Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Art Kendall
The syntax you cite is Python.  Python may or not be necessary. Exactly what to do depends on why you are doing this. To save storage space?

Do you want to delete variable contents that are more than 500 character?

Do you want to delete variables themselves that have more than 500 characters?

first see ALTER TYPE  under <help>
(all =a)  will set the length for the whole variable to the length of the longest contained value.

see the <help> on string function CHAR.LENGTH .


Art Kendall
Social Research Consultants
On 5/11/2014 8:44 AM, Kanch [via SPSSX Discussion] wrote:
Hi I am new to SPSS and I am looking for a syntax to delete variables than have width more than 500.

I came across the syntax dict1 = spssaux.VariableDict(variableType='string'). I am not sure if it will be helpful. It'll be great if anyone could help me with this! Thanks!


To start a new topic under SPSSX Discussion, email [hidden email]
To unsubscribe from SPSSX Discussion, click here.
NAML

Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Kanch
Hi Art,
        I want to delete all string variables themselves that are more than 500 characters. So far I have only managed to find the syntax to delete all string variables and variables that begin with BP.

begin program.
import spss, spssaux
dict2=spssaux.VariableDict(pattern='^BP')
dict1=spssaux.VariableDict(variableType='string')
vars1 = " ".join(dict1.variables)
vars2 = " ".join(dict2.variables)
dropvars=vars1+ " " +vars2
spss.Submit("Delete Variable " + dropvars)
end program.

Please do let me know how I can perhaps modify this to include the condition that the string variables that have to be deleted have width more than 500.


Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

David Marso
Administrator
Maybe look in the spss/python API docs and see if there is an attribute called VariableWidth (sp) or some such logical variation and then look for some python comparison symbol which means greater than?
Put the two together and bingo!

Kanch wrote
Hi Art,
        I want to delete all string variables themselves that are more than 500 characters. So far I have only managed to find the syntax to delete all string variables and variables that begin with BP.

begin program.
import spss, spssaux
dict2=spssaux.VariableDict(pattern='^BP')
dict1=spssaux.VariableDict(variableType='string')
vars1 = " ".join(dict1.variables)
vars2 = " ".join(dict2.variables)
dropvars=vars1+ " " +vars2
spss.Submit("Delete Variable " + dropvars)
end program.

Please do let me know how I can perhaps modify this to include the condition that the string variables that have to be deleted have width more than 500.


Thanks!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

David Marso
Administrator
spss.GetVariableType Function spss.GetVariableType(index). Returns 0 for numeric variables or the defined length for string variables for the variable in the active dataset indicated by the index value. The argument is the index value. Index values represent position in the active dataset, starting with 0 for the first variable in file order.
Example #create separate strings of numeric and string variables numericvars=’’ stringvars=’’ varcount=spss.GetVariableCount() for i in xrange(varcount): if spss.GetVariableType(i) > 0: stringvars=stringvars+""+spss.GetVariableName(i) else: numericvars=numericvars+""+spss.GetVariableName(i)

David Marso wrote
Maybe look in the spss/python API docs and see if there is an attribute called VariableWidth (sp) or some such logical variation and then look for some python comparison symbol which means greater than?
Put the two together and bingo!

Kanch wrote
Hi Art,
        I want to delete all string variables themselves that are more than 500 characters. So far I have only managed to find the syntax to delete all string variables and variables that begin with BP.

begin program.
import spss, spssaux
dict2=spssaux.VariableDict(pattern='^BP')
dict1=spssaux.VariableDict(variableType='string')
vars1 = " ".join(dict1.variables)
vars2 = " ".join(dict2.variables)
dropvars=vars1+ " " +vars2
spss.Submit("Delete Variable " + dropvars)
end program.

Please do let me know how I can perhaps modify this to include the condition that the string variables that have to be deleted have width more than 500.


Thanks!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Jon K Peck
In reply to this post by David Marso
begin program.
import spss, spssaux
vardict = spssaux.VariableDict(variableType="string")
verylong = [v.VariableName for v in vardict if v.VariableType > 500]
if verylong:
    spss.Submit("delete variables " + " ".join(verylong))
end program.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email],
Date:        05/12/2014 12:40 AM
Subject:        Re: [SPSSX-L] SPSS syntax to delete string variables with width              more than              500
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Maybe look in the spss/python API docs and see if there is an attribute
called VariableWidth (sp) or some such logical variation and then look for
some python comparison symbol which means greater than?
Put the two together and bingo!


Kanch wrote
> Hi Art,
>         I want to delete all string variables themselves that are more
> than 500 characters. So far I have only managed to find the syntax to
> delete all string variables and variables that begin with BP.
>
> begin program.
> import spss, spssaux
> dict2=spssaux.VariableDict(pattern='^BP')
> dict1=spssaux.VariableDict(variableType='string')
> vars1 = " ".join(dict1.variables)
> vars2 = " ".join(dict2.variables)
> dropvars=vars1+ " " +vars2
> spss.Submit("Delete Variable " + dropvars)
> end program.
>
> Please do let me know how I can perhaps modify this to include the
> condition that the string variables that have to be deleted have width
> more than 500.
>
>
> Thanks!





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/SPSS-syntax-to-delete-string-variables-with-width-more-than-500-tp5725961p5725971.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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


Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Kanch
In reply to this post by David Marso
Hi David,
          I will try this and let you know!Thanks for your help!(:

Regards,
Kanchanah
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Albert-Jan Roskam
I assume it's string length and not display width?

begin program.
import spss, spssaux
variables = [v for v in spssaux.VariableDict() if v.VariableType > 500]
if variables:
    spss.Submit("add files /file = * /drop = %s." % " ".join(variables))
end program.

Regards,

Albert-Jan




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a

fresh water system, and public health, what have the Romans ever done for us?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




----- Original Message -----

> From: Kanch <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Tuesday, May 13, 2014 12:32 PM
> Subject: Re: [SPSSX-L] SPSS syntax to delete string variables with width              more than              500
>
> Hi David,
>           I will try this and let you know!Thanks for your help!(:
>
> Regards,
> Kanchanah
>
>
>
> --
> View this message in context:
> http://spssx-discussion.1045642.n5.nabble.com/SPSS-syntax-to-delete-string-variables-with-width-more-than-500-tp5725961p5725994.html
>
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (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
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Kanch
In reply to this post by Jon K Peck
Hi Jon,
        This worked!Thank you very much. Do you also know how I can export the variables "verylong" so that I can keep a record?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Kanch
In reply to this post by Jon K Peck
Hi Jon,
         This worked! Thank you very much for your reply. I am also finding
for a way to export the deleted variables (verylong) to either excel or
notepad so that I can keep a record. Is there any syntax I can use to do
this?

Thanks once again for your help!

Regards,
Kanchanah



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/SPSS-syntax-to-delete-string-variables-with-width-more-than-500-tp5725961p5726061.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Andy W
In reply to this post by Kanch
What exactly do you mean by "export" - here is a python program that will create a table of those variables that will be deleted.

*****************************************.
DATA LIST FREE / X1 X2 (2F1.0) A1 (A501) A2 (A600).
BEGIN DATA
1 1 A B
1 1 C D
END DATA.

begin program.
import spss, spssaux
vardict = spssaux.VariableDict(variableType="string")
verylong = [v.VariableName for v in vardict if v.VariableType > 500]
longstr =  [v.VariableType for v in vardict if v.VariableType > 500] #Grabbing string length

spss.StartProcedure("Over500")
table = spss.BasePivotTable("String Vars Over 500", "COMMON")
table.SetDefaultFormatSpec(spss.FormatSpec.Count)
table.SimplePivotTable(rowdim = "Variable",
    rowlabels=verylong,
    collabels=["Length"],
    cells=longstr)
spss.EndProcedure()

#if verylong:
#    spss.Submit("delete variables " + " ".join(verylong))
end program.
*****************************************.

I don't see much point in exporting the actual data - as you shouldn't be overwriting the initial data to begin with.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: SPSS syntax to delete string variables with width more than 500

Andy W
Another sneaky way to do this as well (without Python) is to use ALTER TYPE and SORT VARIABLES (although these were introduced as of V16 - so they won't work going back really far).

What this syntax does is change any variable of length 501 to 502 (which seems like a reasonable concession to me), then creates a variable to note the start of the string, this allows one to then sort the file and then drop all variables between the two variables you created.

**********************************.
ALTER TYPE ALL (A501 = A502).
STRING @Holder (A501).
SORT VARIABLES BY TYPE.
NUMERIC Temp.
DISPLAY DICTIONARY /VARIABLES = @Holder to Temp.
MATCH FILES FILE = * /DROP @Holder TO Temp.
**********************************.

If the ordering of the variables is important to keep you can use the VARIABLE ATTRIBUTE command - although that is not convenient like the Python solution.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/