|
Dear all,
I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet. begin program. import spss for i in range (3,7): spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.") end program. TIA, Ruben van den Berg Consultant Models & Methods TNS NIPO Email: [hidden email] Mobiel: +31 6 24641435 Telefoon: +31 20 522 5738 Internet: www.tns-nipo.com New Windows 7: Simplify what you do everyday. Find the right PC for you. |
|
Hi!
Try to Change: spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.") To: spss.Submit("crosstabs " + spss.GetVariableName(i) + " by gender/cells=column.") All the best Wilhelm (Wille) Landerholm
+46-735-460000 Queue/STATB BOX 92 162 12 Vallingby Sweden http://www.qsweden.com http://www.statb.com QUEUE/STATB - your partner in data analysis, data modeling and data mining. Ruben van den Berg skrev 2010-06-11 14:53: Dear all, |
|
Oh, how stupid of me! Time for weekend I guess...
Thanks! Ruben van den Berg Consultant Models & Methods TNS NIPO Email: [hidden email] Mobiel: +31 6 24641435 Telefoon: +31 20 522 5738 Internet: www.tns-nipo.com Date: Fri, 11 Jun 2010 15:25:24 +0200 From: [hidden email] To: [hidden email] CC: [hidden email] Subject: Re: Very basic Python loop Hi! Try to Change: spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.") To: spss.Submit("crosstabs " + spss.GetVariableName(i) + " by gender/cells=column.") All the best Wilhelm (Wille) Landerholm +46-735-460000 Queue/STATB BOX 92 162 12 Vallingby Sweden http://www.qsweden.com http://www.statb.com QUEUE/STATB - your partner in data analysis, data modeling and data mining. Ruben van den Berg skrev 2010-06-11 14:53: Dear all, Express yourself instantly with MSN Messenger! MSN Messenger |
|
In reply to this post by Ruben Geert van den Berg
Try this: begin program. import spss varlist=[] varcount=spss.GetVariableCount() for i in xrange(varcount): varlist.append(spss.GetVariableName(i)) for i in range(3,7): cmd="crosstabs tables=" + varlist[i] + " by gender /cells=column." spss.Submit(cmd) end program. But remember that python indexing starts at 0, so the first variable in this range is, in fact, the fourth variable in the file.
Dear all, I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet. begin program. import spss for i in range (3,7): spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.") end program. TIA, Ruben van den Berg Consultant Models & Methods TNS NIPO Email: [hidden email] Mobiel: +31 6 24641435 Telefoon: +31 20 522 5738 Internet: www.tns-nipo.com New Windows 7: Simplify what you do everyday. Find the right PC for you. |
|
In reply to this post by Ruben Geert van den Berg
You are close. See below. Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
Dear all, I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet. begin program. import spss for i in range (3,7): spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.") end program. Submit needs to build a single command string. You are passing the parts as three arguments. Instead, do this spss.Submit("crosstabs " + spss.GetVariableName(i) + " by gender/cells=column.") That will concatenate the parts into a single string and submit the command to Statistics. HTH, Jon Peck TIA, Ruben van den Berg Consultant Models & Methods TNS NIPO Email: [hidden email] Mobiel: +31 6 24641435 Telefoon: +31 20 522 5738 Internet: www.tns-nipo.com New Windows 7: Simplify what you do everyday. Find the right PC for you. |
|
| Free forum by Nabble | Edit this page |
