Hi, I came across an Python error. I am totally new on Python programming, your inputs would be very appreciated. Thanks.BEGIN PROGRAM. import spss spss.StartDataStep() datasetObj1=spss.Dataset(name="d1") datasetObj2=spss.Dataset(name="d2") nvars=len(datasetObj1)+1 datasetObj1.varlist.append('Python') for i in range (len(datasetObj1.cases)): for j in range (len(datasetObj2.cases)): if (datasetObj1.cases[i,3]==datasetObj2.cases[j,0]): if (datasetObj2.cases[j,4] != None): if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3] or datasetObj1.cases[i,17]==datasetObj1.cases[j,4]): datasetObj1.cases[i, nvars]=1 else: if (datasetObj2.cases[j,4] == None): if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3]): datasetObj1.cases[i, nvars]=1 spss.EndDataStep() END PROGRAM. |
These calls specify creating a Python dataset object for
open SPSS datasets named d1 and d2. The dataset name must already
be associated with an SPSS dataset, so the error is indicating that either
d1 or d2 does not correspond to an SPSS dataset. Also be sure that
the case of the names matches the case in Statistics.
datasetObj1=spss.Dataset(name="d1") datasetObj2=spss.Dataset(name="d2") Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: huang jialin <[hidden email]> To: [hidden email], Date: 03/21/2013 09:27 AM Subject: [SPSSX-L] Python Error spss.errMsg.SpssError: [errLevel 99] Cannot create specified dataset. Sent by: "SPSSX(r) Discussion" <[hidden email]> Hi, I came across an Python error. I am totally new on Python programming, your inputs would be very appreciated. Thanks. BEGIN PROGRAM. import spss spss.StartDataStep() datasetObj1=spss.Dataset(name="d1") datasetObj2=spss.Dataset(name="d2") nvars=len(datasetObj1)+1 datasetObj1.varlist.append('Python') for i in range (len(datasetObj1.cases)): for j in range (len(datasetObj2.cases)): if (datasetObj1.cases[i,3]==datasetObj2.cases[j,0]): if (datasetObj2.cases[j,4] != None): if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3] or datasetObj1.cases[i,17]==datasetObj1.cases[j,4]): datasetObj1.cases[i, nvars]=1 else: if (datasetObj2.cases[j,4] == None): if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3]): datasetObj1.cases[i, nvars]=1 spss.EndDataStep() END PROGRAM. |
Administrator
|
In reply to this post by huang jialin
First of all. Please explain what in the heck you are attempting to do.
COMMENTS IN CODE are not just a nicety. They are essential especially when you have nested stuff like this. Where is my sharpened spoon? --
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?" |
In reply to this post by Jon K Peck
Hi Jon, Thanks for your response. I changed the dataset names into DataSet1, and DataSet4, but got another warning:Traceback (most recent call last): File "<string>", line 4, in <module> File "C:\Python26\lib\site-packages\spss190\spss\dataStep.py", line 102, in __init__ raise SpssError,error spss.errMsg.SpssError: [errLevel 91] A dataset with the same name already exists. On Thu, Mar 21, 2013 at 11:00 AM, Jon K Peck <[hidden email]> wrote: These calls specify creating a Python dataset object for open SPSS datasets named d1 and d2. The dataset name must already be associated with an SPSS dataset, so the error is indicating that either d1 or d2 does not correspond to an SPSS dataset. Also be sure that the case of the names matches the case in Statistics. |
In reply to this post by David Marso
David, I am trying to compare two dataset and save the results into one dataset. For example, the layout of d1 is ID ... V1 V2 V3; d2 is ID V11 V12 V13 V2. I am not sure whether the following code makes more sense now. Thanks.BEGIN PROGRAM. import spss spss.StartDataStep() datasetObj1=spss.Dataset(name="DataSet4") datasetObj2=spss.Dataset(name="DataSet1") nvars=len(datasetObj1)+1 datasetObj1.varlist.append('T1JobCodePython') for i in range (len(datasetObj1.cases)): #loop though all cases in d1 for j in range (len(datasetObj2.cases)): #loop though all cases in d2 if (datasetObj1.cases[i,3]==datasetObj2.cases[j,0]): # if d1 ID=d2 ID if (datasetObj2.cases[j,4] != None): #if d2 v13<>" " if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3] or datasetObj1.cases[i,17]==datasetObj1.cases[j,4]): #if d1 v1=d2 v11 or d1 v1=d2 v12 or d1v1=d2 v13 datasetObj1.cases[i, nvars]=1 #creat a varible, and assign 1. else: if (datasetObj2.cases[j,4] == None): #if d2 v13=" " if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3]): #if d1 v1=d2 v11 or d1 v1=d2 v12 datasetObj1.cases[i, nvars]=1 #assign 1. spss.EndDataStep() END PROGRAM. On Thu, Mar 21, 2013 at 11:03 AM, David Marso <[hidden email]> wrote: First of all. Please explain what in the heck you are attempting to do. |
In reply to this post by huang jialin
If you have not called EndDataStep, perhaps
because of an error, and you run the code again, you will get that error.
For example, if you run this code twice, it will work (assuming the named datasets exist). If you comment out the EndDataStep call and run it twice, you will see that error. begin program. import spss spss.StartDataStep() ds1 = spss.Dataset(name="DataSet1") ds2 = spss.Dataset(name="DataSet2") print ds1, ds2 spss.EndDataStep() end program. I would concur with David that the actual transformation code is pretty opaque and needs, at least, some commenting. Another thing that would help would be using actual variable names in the calculations. For example, begin program. import spss spss.StartDataStep() ds1 = spss.Dataset(name="DataSet1") v1 = ds1.varlist for i in range(5): print ds1.cases[i, v1["origin"].index] spss.EndDataStep() end program. You could make this more compact and even more readable like this. In your case you might define d1 and d2 functions for the two datasets. begin program. import spss spss.StartDataStep() ds1 = spss.Dataset(name="DataSet1") def d1(i, name): return ds1.cases[i, ds1.varlist[name].index] for i in range(5): print d1(i, "origin") spss.EndDataStep() end program Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: huang jialin <[hidden email]> To: Jon K Peck/Chicago/IBM@IBMUS, Cc: SPSSX-L post <[hidden email]> Date: 03/21/2013 10:18 AM Subject: Re: Python Error spss.errMsg.SpssError: [errLevel 99] Cannot create specified dataset. Hi Jon, Thanks for your response. I changed the dataset names into DataSet1, and DataSet4, but got another warning: Traceback (most recent call last): File "<string>", line 4, in <module> File "C:\Python26\lib\site-packages\spss190\spss\dataStep.py", line 102, in __init__ raise SpssError,error spss.errMsg.SpssError: [errLevel 91] A dataset with the same name already exists. On Thu, Mar 21, 2013 at 11:00 AM, Jon K Peck <peck@...> wrote: These calls specify creating a Python dataset object for open SPSS datasets named d1 and d2. The dataset name must already be associated with an SPSS dataset, so the error is indicating that either d1 or d2 does not correspond to an SPSS dataset. Also be sure that the case of the names matches the case in Statistics. datasetObj1=spss.Dataset(name="d1") datasetObj2=spss.Dataset(name="d2") Jon Peck (no "h") aka Kim Senior Software Engineer, IBM peck@... phone: <a href="tel:720-342-5621" target=_blank>720-342-5621 From: huang jialin <huangpsych@...> To: [hidden email], Date: 03/21/2013 09:27 AM Subject: [SPSSX-L] Python Error spss.errMsg.SpssError: [errLevel 99] Cannot create specified dataset. Sent by: "SPSSX(r) Discussion" <[hidden email]> Hi, I came across an Python error. I am totally new on Python programming, your inputs would be very appreciated. Thanks. BEGIN PROGRAM. import spss spss.StartDataStep() datasetObj1=spss.Dataset(name="d1") datasetObj2=spss.Dataset(name="d2") nvars=len(datasetObj1)+1 datasetObj1.varlist.append('Python') for i in range (len(datasetObj1.cases)): for j in range (len(datasetObj2.cases)): if (datasetObj1.cases[i,3]==datasetObj2.cases[j,0]): if (datasetObj2.cases[j,4] != None): if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3] or datasetObj1.cases[i,17]==datasetObj1.cases[j,4]): datasetObj1.cases[i, nvars]=1 else: if (datasetObj2.cases[j,4] == None): if (datasetObj1.cases[i,17]==datasetObj1.cases[j,2] or datasetObj1.cases[i,17]==datasetObj1.cases[j,3]): datasetObj1.cases[i, nvars]=1 spss.EndDataStep() END PROGRAM. |
Jon, Thanks for your inputs. I appreciate it. On Thu, Mar 21, 2013 at 12:13 PM, Jon K Peck <[hidden email]> wrote: If you have not called EndDataStep, perhaps because of an error, and you run the code again, you will get that error. |
Administrator
|
In reply to this post by huang jialin
"I am not sure whether the following code makes more sense now. "
Maybe a little bit. I could easily decipher it if I wanted to take the time to bother with it. If this is merely a learning exercise then carry on (but do your self a favor: COMMENT THE CODE). If this is intended as some real application to be used then you really need to ask yourself. How scalable is this going to be? It might run fine with 100 cases or even 1000. What happens when somebody hits it with 10000 or 100000 cases? The computer is going to crumble into a shrieking mass of silicon and copper. Element by element comparisons and assignments are REALLY SLOW in any language. I'm not sure what comparisons can be made between SPSS MATRIX and Python, but as an example. Question to Jon. How can the processor time be greater than elapsed time? *****************. ** Program 1. **. *********************************************************************. ** Calculate the sum of the values in an array using MSUM function **. *********************************************************************. MATRIX. COMPUTE V=UNIFORM(1000,1000). COMPUTE X=MSUM(V) . PRINT X / FORMAT "F10.5". END MATRIX. ********************************************************************. ************** Processor time: 00:00:00.05 **********************. ************** Elapsed time : 00:00:00.04 **********************. ***************************** END PROGRAM 1 **********************. ********************************************************************. *****************. ** Program 2. **. **********************************************************************. ** Calculate the sum of the values in an array using painfully slow **. ** and tedious element by element addition **. **********************************************************************. SET MXLOOPS 1000000. MATRIX. COMPUTE V=UNIFORM(1000,1000). COMPUTE X=0. LOOP #=1 TO NROW(V). LOOP ##=1 TO NCOL(V). COMPUTE X=X+V(#,##). PRINT X. END LOOP. END LOOP. END MATRIX. ********************************************************************. ************** Processor time: 00:00:17.93 ************************. ************** Elapsed time : 00:00:14.25 ************************. ***************************** END PROGRAM 2 **********************. ********************************************************************.
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?" |
In reply to this post by huang jialin
<help> <compare> yields
Comparing datasetsCompare Datasets compares the active dataset to another dataset in the current session or an external file in IBM® SPSS® Statistics format. To compare datasetsOpen a data file and make sure it is the active dataset. (You can make a dataset the active dataset by clicking on the Data Editor window for that dataset.) From the menus choose: Data > Compare Datasets Select the open dataset or SPSS Statistics data file that you want to compare to the active dataset. Select one or more fields (variables) that you want to compare. Optionally, you can: • Match cases (records) based on one or more case ID values. • Compare data dictionary properties (field and value labels, user-missing values, measurement level, etc). • Create a flag field in the active dataset that identifies mismatched cases. • Create new datasets that contain only matched cases or only mismatched cases. Related TopicsArt Kendall Social Research ConsultantsOn 3/21/2013 12:37 PM, huang jialin [via SPSSX Discussion] wrote:
Art Kendall
Social Research Consultants |
Administrator
|
I doubt that COMPARE DATASETS is going to do that Cartesian business.----
------ From what I can tell of that uncommented masterpiece of spaghettios;-( OP appears to be attempting to compare every case in one file to every case in a second file and then do something or another which I didn't bother to process due to the overwhelming urge to extract my eyeballs with a sharpened spoon! <RANT> And I thought all this time other people's Visual Basic code could look hideous. Python appears to provide a whole new level of WTF capabilities (can't wait to jump into the snake pit). I've recently been staring at some poor soul's attempt to translate a bunch of OLD AS FORTRAN algos into VBA... WOW every GOTO is still in the translation (at least it works, but what the H? -have a little self respect-). Also been making an (-a$$) of myself with that operator precedence issue, so I'm a little cranky this AM. </RANT>
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?" |
I just took a SWAG that this was a
common file comparison to check on data entry.
For a couple of decades I urged SPSS to have a way to compare datasets. However, I have never tried using that procedure without previously sorting cases and unduplicating the files. Perhaps the OP was comparing each case because the cases were not sorted. Given that the variables are in different orders I might SORT VARIABLES. But I think the procedure can handle the variables being in different orders. I have not tried that. Art Kendall Social Research ConsultantsOn 3/28/2013 10:38 AM, David Marso [via SPSSX Discussion] wrote: I doubt that COMPARE DATASETS is going to do that Cartesian business.----
Art Kendall
Social Research Consultants |
Free forum by Nabble | Edit this page |