changing data declaration

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

changing data declaration

Scott Roesch
Hi all:

Does anyone have some quick code that will convert variables from String
to Numeric? I have a number of variables that need to be converted, and I
do not want to manually do this in the Variable View window if I can help
it. Thanks in advance!

Scott

=====================
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: changing data declaration

Albert-Jan Roskam
Hi,

Basically the syntax is
COMPUTE numvar = NUMBER(strvar, F5).
This assumes that there are no letters etc in the string fields.  The Python syntax below converts all string variables in a given dataset to their numerical equivalent. The original var name is retained.

Cheers!!
Albert-Jan

set mprint = on.

begin program.
import spss, random
varcnt = spss.GetVariableCount()
tmpvar = "tmp" + str(random.random())
strlist = []
for i in range (varcnt):
        varname = spss.GetVariableName(i)
        if spss.GetVariableFormat(i).find("A") == 0:
                strlist.append(varname)
for i in range (len (strlist)):
        spss.Submit("rename variables (%s = %s)." % (strlist[i], tmpvar))
        spss.Submit("compute %s = number(%s, f5)." % (strlist[i], tmpvar))
        spss.Submit(""" exe.
                        delete variables %s. """ % (tmpvar))
end program.




--- On Mon, 8/11/08, Scott Roesch <[hidden email]> wrote:

> From: Scott Roesch <[hidden email]>
> Subject: changing data declaration
> To: [hidden email]
> Date: Monday, August 11, 2008, 11:50 PM
> Hi all:
>
> Does anyone have some quick code that will convert
> variables from String
> to Numeric? I have a number of variables that need to be
> converted, and I
> do not want to manually do this in the Variable View window
> if I can help
> it. Thanks in advance!
>
> Scott
>
> =====================
> 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