|
Jon Peck at SPSS was kind enough to point out a few ways that my code
could be simplified, and having received a few inquiries about it from list members, I thought I'd pass along the revised version. Note that the first spss.Submit statement, which was originally in an explicit loop, now sits in an implied loop made possible by the join() method (i.e., looping item by item through listTest, join "sat" and the current item in listTest). Also, the second spss.Submit statement is now significantly easier to read by use of the dictionary at the end of the line and %()s for string substitution. Many thanks to Jon Peck. Now I'll shut up about Python and get back to work ;) BEGIN PROGRAM. import spss listTest = ["EB","MB","CH","CL","EP","FL","FR","GL","GM","IT","JL","KL","LR","LT","M1","M2","MH","PH","SL","SP","UH","WH","WR"] spss.Submit("numeric " + " ".join(["sat" + item for item in listTest])) for i in range(0,len(listTest)): for p in range(1,18): spss.Submit("if(code%(p)s = '%(item)s' and (score%(p)s > sat%(item)s or sysmit(sat%(item)s))) sat%(item)s = score%(p)s ." % {'p': p, 'item': listTest[i]}) spss.Submit("exe.") END PROGRAM. -- Daniel Robertson Senior Research and Planning Associate Institutional Research and Planning Cornell University, Ithaca NY 14853-2801 607.255.9642 / irp.cornell.edu |
|
I will add just two things before we all get back to work. Python has the best for loop on the planet (certainly better than !DO) so it's even easier:
for item in listTest: for p in xrange(1,18): spss.Submit("if(code%(p)s = '%(item)s' and (score%(p)s > sat%(item)s or sysmit(sat%(item)s))) sat%(item)s = score%(p)s ." % {'p': p, 'item': item}) It is almost never necessary to explicitly drive a loop by looking up values by index. In Python you can just give a name to each item in a list. This can take a while to get used to, so it's easy to overlook. (If you do need the loop index for some reason, consider using the enumerate() function.) Also, I've changed range to xrange. The difference is that range expands into a list containing all the integers (from 1 to 17 in this case), while xrange just delivers the next integer in the sequence upon request. John Bauer -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Daniel Robertson Sent: Friday, June 08, 2007 1:29 PM To: [hidden email] Subject: Python success story - Update Jon Peck at SPSS was kind enough to point out a few ways that my code could be simplified, and having received a few inquiries about it from list members, I thought I'd pass along the revised version. Note that the first spss.Submit statement, which was originally in an explicit loop, now sits in an implied loop made possible by the join() method (i.e., looping item by item through listTest, join "sat" and the current item in listTest). Also, the second spss.Submit statement is now significantly easier to read by use of the dictionary at the end of the line and %()s for string substitution. Many thanks to Jon Peck. Now I'll shut up about Python and get back to work ;) BEGIN PROGRAM. import spss listTest = ["EB","MB","CH","CL","EP","FL","FR","GL","GM","IT","JL","KL","LR","LT","M1","M2","MH","PH","SL","SP","UH","WH","WR"] spss.Submit("numeric " + " ".join(["sat" + item for item in listTest])) for i in range(0,len(listTest)): for p in range(1,18): spss.Submit("if(code%(p)s = '%(item)s' and (score%(p)s > sat%(item)s or sysmit(sat%(item)s))) sat%(item)s = score%(p)s ." % {'p': p, 'item': listTest[i]}) spss.Submit("exe.") END PROGRAM. -- Daniel Robertson Senior Research and Planning Associate Institutional Research and Planning Cornell University, Ithaca NY 14853-2801 607.255.9642 / irp.cornell.edu |
|
In reply to this post by Daniel Robertson
Hi all,
Does anyone have some nifty code to tell SPSS to do the following: 1. Create a variable called ID and assign ID numbers 1-315. 2. Create a variable called Group and randomly assign a 1, 2, or 3 to each of the 315 IDs so that 105 cases get a 1, 105, get a 2 and 105 get a 3. I've used UNIFORM to randomly assign a list to either a 1 or 0 and i could easily create these variables and mannually do this. However, seeing the syntax to do this would be valuable for some other tasks like this. Thanks Carol ===================== 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 |
|
Carol,
Here are two nifty solutions, one provided by Jan Spousta, the other by Raynald Levesque and both posted to this list just a few weeks ago. I'm sure that either will prove useful, given slight modifications. Regards, King Douglas American Airlines Customer Research http://www.kingdouglas.com/SPSS.HTM JAN SPOUSTA: Run the following syntax from a Syntax window (Run -> All) ; suppose you have opened a datafile with 96 rows: COMPUTE aux_id = $casenum /*to be able to restore the original order of rows*/. COMPUTE random_number = uniform(1) . SORT CASES BY random_number (A) . compute random_number = $casenum /*to convert the random numbers from <0,1> into 1, 2, 3..., N */. compute random_group = mod(random_number, 3) + 1 /* create 3 groups */. SORT CASES BY aux_id (A) . formats random_group aux_id random_number (f6). var lab random_group "Random group". Results are in the variable random_group. Hope this helps Jan RAYNALD LEVESQUE: Here is one method: * Create a sample file for illustration purspoes. INPUT PROGRAM. LOOP id=1 TO 96 . - END CASE. END LOOP . END FILE. END INPUT PROGRAM. * Do the job. COMPUTE draw=UNIFORM(1). SORT CASES BY draw. RANK VARIABLES=draw /NTILES(3) INTO group . Regards -- Raynald Levesque www.spsstools.net "Parise, Carol A." <[hidden email]> wrote: Hi all, Does anyone have some nifty code to tell SPSS to do the following: 1. Create a variable called ID and assign ID numbers 1-315. 2. Create a variable called Group and randomly assign a 1, 2, or 3 to each of the 315 IDs so that 105 cases get a 1, 105, get a 2 and 105 get a 3. I've used UNIFORM to randomly assign a list to either a 1 or 0 and i could easily create these variables and mannually do this. However, seeing the syntax to do this would be valuable for some other tasks like this. Thanks Carol ===================== 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 |
