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 (December 2007)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 21 Dec 2007 05:31:50 -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:   Python: exporting a file to its string equivalent
In-Reply-To:   <002701c84389$ab712a40$02537ec0$@com>
Content-Type:   text/plain; charset=iso-8859-1

Hi listers,

I am trying to learn Python and to practice I thought it would be nice to write a program that converts all numeric variables to string. This might come in handy when I use disease code classification and I export those to xls. I've noticed several times that Excel has the annoying habit to 'abbreviate' e.g. code 00001 to 1. This could have a completely different meaning!

For now I want to convert F and N type variables to their string equivalents. Maybe the code below is overly complicated already, but I just wanted to try the various Python commands.

I have some questions about the code I wrote:

Ad ## 1 ## Not all variables are converted to string. It's like the IF-ELIF jumps too soon to the next step.

How come?

Ad ## 2 ## Minor nuisance: how can the list of variables be numbered? I thought "enumerate" was the way to do it, but maybe not in this case?

Ad ## 3 ## How does string replacement work in this case? Why are the double backslashes needed? This complicates things.

Could you help me with this? Any other, general remarks?

Thanks in advance!

Albert-Jan

*******************. ** sample file input program. set seed = 12262007. + loop #i=1 to 100. + numeric n1 n2 n3 (n8.0). + compute f = abs(rnd(rv.normal(10,5))). + compute n1 = abs(rnd(rv.normal(10,5))). + compute n2 = abs(rnd(rv.normal(5,5))). + compute n3 = abs(rnd(rv.normal(0,5))). + end case. + end loop. + end file. end input program. exe. save outfile = 'd:\temp\testit.sav'.

*******************. ** Actual program.

BEGIN PROGRAM. import spss, os def Num2String(file): """ Convert all numeric values of an SPSS sav file to string and write entire file to num2string.xls. Handy for e.g. disease codes because code '00001' would not be converted to '1' by Excel. """ try: # open file and build tuple of numeric vars. spss.Submit("get file = %(myfile)s ." %{'myfile' : file}) numericvars=[ ] for i in range(spss.GetVariableCount()): if spss.GetVariableType(i) == 0:

numericvars.append(spss.GetVariableName(i)) for j in range(len(numericvars)): k = numericvars[j] # convert F vars to string. if spss.GetVariableFormat(j).find("F")==0: ## 1 ##

spss.Submit(r""" string temp (a10). compute temp = string(%(k)s,f8). exe. delete variables %(k)s. rename variables (temp = %(k)s). """ %locals()) # convert N vars to string. elif spss.GetVariableFormat(j).find("N")==0: spss.Submit(r""" string temp (a10). compute temp = string(%(k)s,n8). exe. delete variables %(k)s. rename variables (temp = %(k)s). """ %locals()) # copy variable labels to 'stringed' file and export to xls. spss.Submit("apply dictionary from %(myfile)s / varinfo varlabel." %{'myfile' : file}) spss.Submit(r"""save translate / outfile = 'd:\temp\num2string.xls' / type = xls / version = 8 / fieldnames / replace.""") print "The following numeric vars were converted to string format: " for x, y in enumerate(range(len(numericvars))): print y, "\n".join(numericvars) ## 2 ##

break # print something if file extension is not sav, and if file or path does not exist. except: if file[-4:-1] != 'sav': print "File extension is " + str.upper(file[-4:-1]) "\n This is probably not an SPSS sav file." else: if os.path.exists('d:\\temp\\employee data.sav') == False: ## 3 ## print r"Input path and/or file do not exist. Try again, dude." # Num2String(r"'c:\program files\spss\employee data.sav'") Num2String(r"'d:\temp\testit.sav'") END PROGRAM.

Cheers! Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Did you know that 87.166253% of all statistics claim a precision of results that is not justified by the method employed? [HELMUT RICHTER] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

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