|
Hello.
I am working with SPSS 15.0.1 (I moved from version 14.0.1 yesterday), and I am using the Python language to build program blocks (within SPSS syntax). I presume that some of you know about this feature. I am interested in working with a cursor (using the 'Cursor' class available from the 'spss' module) in the following way: I want to read data from the active data set, one at a time, in sequential order, so I should use the Cursor class in "read" mode, and use "fetchone" method. My idea is this: building a loop in Python; in each iteration of that loop, I read one "line" (row) of the active data set (using the "fetchone" method of the cursor for that), perform some computations with those data, and then, with the results of those auxiliary computations (and using also information of previous rows, that will have been stored in previous iterations using auxiliary variables), UPDATE one given row of the active data set in fact, the same row that is being read at that iteration. So, the idea behind this is that I want/need to update each row in the active data set using information of that very same row and also information of some previous rows. That's why I think I need to use cursors (for me, it's the natural or logical approach, from the point of view of database management). The problem is that I don't know how to perform exactly this procedure (particularly, the part of UPDATING the 'active row' in each iteration of the loop), as I don't have much experience in program blocks in SPSS. Can anyone give me some hints, examples, help...? Thank you very much in advance. Also, thank you for the answers I received to all my previous questions, which have been very useful to me. -- Vicent Giner Bosch ESTADIS - Statistics, Data Mining and Optimization Group Polytechnic University of Valencia Department of Applied Statistics, Operations Reseach and Quality Camí de Vera, s/n 46022 Valencia, Spain Tel.: +34 96 387 7490 / +34 96 387 7007 - Ext. 74948 / +34 630306621 74948 Fax: +34 96 387 7499 ====================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 |
|
Here is a simple example of creating a new casewise variable calculated from existing variables. This uses the spssdata auxiliary module that you can download from SPSS Developer Central (www.spss.com/devcentral), because that simplifies the use of cursors considerably over what is in the spss module. You will also need spssaux.py and namedTuple.py from Dev Central. (You can just put these files in \python24\lib\site-packages to make them accessible.)
In SPSS 15, you can add new variables, but you cannot modify existing ones, so this example creates a new variable which is the average of salary and salbegin (in the already opened employee data.sav file shipped with SPSS). In this example, all the variables are fetched into Python. The use of the “for case in curs” line implicitly calls fetchone for each case and then sets the value. By using the spssdata cursor, you can refer to the case variables by name, but you can also index by position as case[i]. begin program. import spss, spssdata curs = spssdata.Spssdata(accessType='w') try: curs.append("aNewVariable") curs.commitdict() for case in curs: newval = case.salary - case.salbegin curs.casevalues([newval]) finally: curs.CClose() end program. HTH, Jon Peck -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Vicent Giner Bosch Sent: Friday, December 28, 2007 5:42 AM To: [hidden email] Subject: [SPSSX-L] Handling cursors Hello. I am working with SPSS 15.0.1 (I moved from version 14.0.1 yesterday), and I am using the Python language to build program blocks (within SPSS syntax). I presume that some of you know about this feature. I am interested in working with a cursor (using the 'Cursor' class available from the 'spss' module) in the following way: I want to read data from the active data set, one at a time, in sequential order, so I should use the Cursor class in "read" mode, and use "fetchone" method. My idea is this: building a loop in Python; in each iteration of that loop, I read one "line" (row) of the active data set (using the "fetchone" method of the cursor for that), perform some computations with those data, and then, with the results of those auxiliary computations (and using also information of previous rows, that will have been stored in previous iterations using auxiliary variables), UPDATE one given row of the active data set —in fact, the same row that is being read at that iteration. So, the idea behind this is that I want/need to update each row in the active data set using information of that very same row and also information of some previous rows. That's why I think I need to use cursors (for me, it's the natural or logical approach, from the point of view of database management). The problem is that I don't know how to perform exactly this procedure (particularly, the part of UPDATING the 'active row' in each iteration of the loop), as I don't have much experience in program blocks in SPSS. Can anyone give me some hints, examples, help...? Thank you very much in advance. Also, thank you for the answers I received to all my previous questions, which have been very useful to me. -- Vicent Giner Bosch ESTADIS - Statistics, Data Mining and Optimization Group Polytechnic University of Valencia Department of Applied Statistics, Operations Reseach and Quality Camí de Vera, s/n 46022 Valencia, Spain Tel.: +34 96 387 7490 / +34 96 387 7007 - Ext. 74948 / +34 630306621 74948 Fax: +34 96 387 7499 ======= 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 |
|
This is very helpful for me! I think Python syntax is just what SPSS was
needing, but it's a bit complicated at first... Particularly, I was willing to have cursors handling in SPSS syntax, and now it is possible! :-) I am going to try your hint. Thank you very much. -- Vicent Missatge citat per "Peck, Jon" <[hidden email]>: > Here is a simple example of creating a new casewise variable calculated from > existing variables. This uses the spssdata auxiliary module that you can > download from SPSS Developer Central (www.spss.com/devcentral), because that > simplifies the use of cursors considerably over what is in the spss module. > You will also need spssaux.py and namedTuple.py from Dev Central. (You can > just put these files in \python24\lib\site-packages to make them accessible.) > > In SPSS 15, you can add new variables, but you cannot modify existing ones, > so this example creates a new variable which is the average of salary and > salbegin (in the already opened employee data.sav file shipped with SPSS). > In this example, all the variables are fetched into Python. The use of the > âfor case in cursâ line implicitly calls fetchone for each case and then > sets the value. By using the spssdata cursor, you can refer to the case > variables by name, but you can also index by position as case[i]. > > begin program. > import spss, spssdata > > curs = spssdata.Spssdata(accessType='w') > try: > curs.append("aNewVariable") > curs.commitdict() > for case in curs: > newval = case.salary - case.salbegin > curs.casevalues([newval]) > finally: > curs.CClose() > end program. > > HTH, > Jon Peck > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of > Vicent Giner Bosch > Sent: Friday, December 28, 2007 5:42 AM > To: [hidden email] > Subject: [SPSSX-L] Handling cursors > > Hello. > > > > I am working with SPSS 15.0.1 (I moved from version 14.0.1 yesterday), and I > am using the Python language to build program blocks (within SPSS syntax). I > presume that some of you know about this feature. > > > > I am interested in working with a cursor (using the 'Cursor' class available > from the 'spss' module) in the following way: > > > > I want to read data from the active data set, one at a time, in sequential > order, so I should use the Cursor class in "read" mode, and use "fetchone" > method. > > > > My idea is this: building a loop in Python; in each iteration of that loop, > I read one "line" (row) of the active data set (using the "fetchone" method > of the cursor for that), perform some computations with those data, and > then, with the results of those auxiliary computations (and using also > information of previous rows, that will have been stored in previous > iterations using auxiliary variables), UPDATE one given row of the active > data set âin fact, the same row that is being read at that iteration. > > > > So, the idea behind this is that I want/need to update each row in the > active data set using information of that very same row and also information > of some previous rows. That's why I think I need to use cursors (for me, > it's the natural or logical approach, from the point of view of database > management). > > > > The problem is that I don't know how to perform exactly this procedure > (particularly, the part of UPDATING the 'active row' in each iteration of > the loop), as I don't have much experience in program blocks in SPSS. > > > > Can anyone give me some hints, examples, help...? > > > > Thank you very much in advance. > > > > Also, thank you for the answers I received to all my previous questions, > which have been very useful to me. > > > > -- > Vicent Giner Bosch > > ESTADIS - Statistics, Data Mining and Optimization Group > > Polytechnic University of Valencia > > Department of Applied Statistics, Operations Reseach and Quality > > Camà de Vera, s/n > > 46022 Valencia, Spain > > Tel.: +34 96 387 7490 / +34 96 387 7007 - Ext. 74948 / +34 630306621 > 74948 > > Fax: +34 96 387 7499 > > ======= > 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 |
| Free forum by Nabble | Edit this page |
