Hello!
I'm trying to find a solution for my problem for several days now, I would really appreciate if you could help me. I'm trying to do something like this: Loop #i=1 to 14. Do If Concat('Status_Sem_',#i) ='Z'. Loop #y=2 to 14. Do If not Missing(Concat('Status_Sem_',#i,)) Compute Concat('Status_Sem_',#i) ='P'. End if. end loop. end if. end loop. Excessive googleing brought me to the idea to do it in the macro/python language, but unfortunately I have no idea how to do that :-/. I also tried to make a workaround which won't work because SPSS doesn't allow nested do-repeat-loops: Do Repeat StatusSemesterVariable = Status_Sem_1 to Status_Sem_14. Do If StatusSemesterVariable ='Z'. Compute StatusSemesterVariable ='O'. Do Repeat StatusSemesterVariable Status_Sem_2 Status_Sem_14. Do if StatusSemesterVariable ='Z' OR StatusSemesterVariable='I' OR StatusSemesterVariable ='X' OR StatusSemesterVariable='G'. Compute StatusSemesterVariable='P'. Else. Compute StatusSemesterVariable='Z'. End if. End Repeat. End if. End Repeat. Thank you so much for your help!! Best, Julian -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Loop-through-variable-names-tp5725981.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== 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 |
Administrator
|
Maybe you should explain exactly what you are trying to do rather than dysfunctional code which won't work for a number of reasons?
Meanwhile look into the VECTOR command!
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?" |
Thank you so much. The vector-hint brought the solution! :)
Sorry for not explaining my plans properly, I thought the (admittedly quite defective) code was the clearest way to show my intentions. The plan was to replace all the 'Z's with 'P's in a variable, if the following variables are not empty. Because I had problems using the 'missing' function I had to go with a uncomely solution. But I'm more than happy it works now (see below for the code). Thanks a lot again! Vector Status_Sem_= Status_Sem_1 to Status_Sem_14. Loop #i=1 to 14. Do If Status_Sem_(#i) ='Z'. Loop #y=1 to 14. Do If Status_Sem_(#y+#i) ='Z' OR Status_Sem_(#y+#i) ='I' OR Status_Sem_(#y+#i) ='G' OR Status_Sem_(#y+#i) ='X' OR Status_Sem_(#y+#i) ='P' OR Status_Sem_(#y+#i) ='U'. Compute Status_Sem_(#i)='P'. End if. end loop. end if. end loop. On Mon, 12 May 2014 08:59:26 -0700, David Marso <[hidden email]> wrote: >Maybe you should explain *exactly* what you are trying to do rather than >dysfunctional code which won't work for a number of reasons? >Meanwhile look into the VECTOR command! > > >DrQuixote wrote >> Hello! >> I'm trying to find a solution for my problem for several days now, >> I would really appreciate if you could help me. >> >> I'm trying to do something like this: >> Loop #i=1 to 14. >> Do If Concat('Status_Sem_',#i) ='Z'. >> Loop #y=2 to 14. >> Do If not Missing(Concat('Status_Sem_',#i,)) >> Compute Concat('Status_Sem_',#i) ='P'. >> End if. >> end loop. >> end if. >> end loop. >> >> Excessive googleing brought me to the idea to do it in the macro/python >> language, but >> unfortunately I have no idea how to do that :-/. >> >> I also tried to make a workaround which won't work because SPSS doesn't >> allow nested do-repeat-loops: >> >> Do Repeat >> StatusSemesterVariable = Status_Sem_1 to Status_Sem_14. >> Do If StatusSemesterVariable ='Z'. >> Compute StatusSemesterVariable ='O'. >> Do Repeat StatusSemesterVariable Status_Sem_2 Status_Sem_14. >> Do if StatusSemesterVariable ='Z' OR StatusSemesterVariable='I' OR >> StatusSemesterVariable ='X' OR StatusSemesterVariable='G'. >> Compute StatusSemesterVariable='P'. >> Else. >> Compute StatusSemesterVariable='Z'. >> End if. >> End Repeat. >> End if. >> End Repeat. >> >> Thank you so much for your help!! >> Best, >> >> Julian > > > > > >----- >Please reply to the list and not to my personal email. >Those desiring my consulting or training services please feel free to email >--- >"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?" >-- >View this message in context: http://spssx- discussion.1045642.n5.nabble.com/looping-trough-variable-names- tp5725982p5725984.html >Sent from the SPSSX Discussion mailing list archive at Nabble.com. > >===================== >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 |
Julian,
It sounds like you got what you want but what you describe wanting is not what is implemented in the code. AND there is a even simpler way to do this. You said: " The plan was to replace all the 'Z's with 'P's in a variable, if the following variables are not empty." It looks like your variables are A1. Think about analyzing the vector from right to left rather than from left to right. Your names are too long to write repeatedly so I'm substituting y for your variable names. As I understand your problem, I think the following will do what you want. I assume that the possible values for y(i) are G,X,P,U, or ' '. Basically, G, X, P, and ' ' are left alone. Consider. y1 y2 y3 y4 y5 y6 A A Z Z A A A Z Z A Z A A A A A A A A Z Z A A A A Z Z Z Number of cases read: 6 Number of cases listed: 6 VECTOR Y=Y1 TO Y6. compute #blanks=0. Loop #i=6 to 1 by -1. If (y(#i) eq ' ') #blanks=1. If (#blanks eq 0 and y(#i)='Z') y(#i)='P'. End loop. LIST Y1 TO Y6. y1 y2 y3 y4 y5 y6 A A P P A A A Z Z A Z A A A A A A A A P P A A A A P P P Number of cases read: 6 Number of cases listed: 6 -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of [hidden email] Sent: Tuesday, May 13, 2014 4:03 AM To: [hidden email] Subject: Re: looping trough variable-names Thank you so much. The vector-hint brought the solution! :) Sorry for not explaining my plans properly, I thought the (admittedly quite defective) code was the clearest way to show my intentions. The plan was to replace all the 'Z's with 'P's in a variable, if the following variables are not empty. Because I had problems using the 'missing' function I had to go with a uncomely solution. But I'm more than happy it works now (see below for the code). Thanks a lot again! Vector Status_Sem_= Status_Sem_1 to Status_Sem_14. Loop #i=1 to 14. Do If Status_Sem_(#i) ='Z'. Loop #y=1 to 14. Do If Status_Sem_(#y+#i) ='Z' OR Status_Sem_(#y+#i) ='I' OR Status_Sem_(#y+#i) ='G' OR Status_Sem_(#y+#i) ='X' OR Status_Sem_(#y+#i) ='P' OR Status_Sem_(#y+#i) ='U'. Compute Status_Sem_(#i)='P'. End if. end loop. end if. end loop. On Mon, 12 May 2014 08:59:26 -0700, David Marso <[hidden email]> wrote: >Maybe you should explain *exactly* what you are trying to do rather >than dysfunctional code which won't work for a number of reasons? >Meanwhile look into the VECTOR command! > > >DrQuixote wrote >> Hello! >> I'm trying to find a solution for my problem for several days now, I >> would really appreciate if you could help me. >> >> I'm trying to do something like this: >> Loop #i=1 to 14. >> Do If Concat('Status_Sem_',#i) ='Z'. >> Loop #y=2 to 14. >> Do If not Missing(Concat('Status_Sem_',#i,)) >> Compute Concat('Status_Sem_',#i) ='P'. >> End if. >> end loop. >> end if. >> end loop. >> >> Excessive googleing brought me to the idea to do it in the >> macro/python language, but unfortunately I have no idea how to do >> that :-/. >> >> I also tried to make a workaround which won't work because SPSS >> doesn't allow nested do-repeat-loops: >> >> Do Repeat >> StatusSemesterVariable = Status_Sem_1 to Status_Sem_14. >> Do If StatusSemesterVariable ='Z'. >> Compute StatusSemesterVariable ='O'. >> Do Repeat StatusSemesterVariable Status_Sem_2 Status_Sem_14. >> Do if StatusSemesterVariable ='Z' OR StatusSemesterVariable='I' OR >> StatusSemesterVariable ='X' OR StatusSemesterVariable='G'. >> Compute StatusSemesterVariable='P'. >> Else. >> Compute StatusSemesterVariable='Z'. >> End if. >> End Repeat. >> End if. >> End Repeat. >> >> Thank you so much for your help!! >> Best, >> >> Julian > > > > > >----- >Please reply to the list and not to my personal email. >Those desiring my consulting or training services please feel free to >--- >"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?" >-- >View this message in context: http://spssx- discussion.1045642.n5.nabble.com/looping-trough-variable-names- tp5725982p5725984.html >Sent from the SPSSX Discussion mailing list archive at Nabble.com. > >===================== >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 ===================== 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 |
Administrator
|
Pretty sure your code does not do what you describe you want and will give tons of warnings about out of range subscripts.
Other than that, maybe you should post a snapshot of before and after of a few cases of data? Rather than Do If Status_Sem_(#y+#i) ='Z' OR Status_Sem_(#y+#i) ='I' OR Status_Sem_(#y+#i) ='G' OR Status_Sem_(#y+#i) ='X' OR Status_Sem_(#y+#i) ='P' OR Status_Sem_(#y+#i) ='U'. Compute Status_Sem_(#i)='P'. End if. Consider IF ANY(Status_Sem_(#y+#i),"Z","I","G","X","P","U") Status_Sem_(#i)='P'. Alternatively IF INDEX(Status_Sem_(#y+#i),"ZIGXPU",1) GT 0 Status_Sem_(#i)='P'.
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 DrQuixote
At 11:33 AM 5/12/2014, DrQuixote wrote:
>I'm trying to do something like this: >Loop #i=1 to 14. >. Do If Concat('Status_Sem_',#i) ='Z'. >. Loop #y=2 to 14. >. Do If not Missing(Concat('Status_Sem_',#i,)) >. Compute Concat('Status_Sem_',#i) ='P'. >. End if. >. end loop. >. end if. >end loop. You probably have your logic wrong here -- the inner-loop index "#y" is never referenced. I can't figure out just what you are trying to do, so I won't try to fix that. However, if your variables "Status_Sem1" to "Status_Sem14" are contiguous in your file, you can do this easily with VECTOR. Like this -- code not tested, AND it retains the logic error I've noted above; VECTOR Status_Sem = Status_Sem1 TO Status_Sem14. Loop #i=1 to 14. . Do If Status_Sem(#i) ='Z'. . Loop #y=2 to 14. . Do If not Missing(Status_Sem(#i)). . Compute Status_Sem(#i) ='P'. . End if. . end loop. . end if. end loop. ===================== 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 |
In reply to this post by David Marso
thanks again for your help.
maybe I wasn't able to explain what I wanted because of my poor english. but i'm 100% sure the code did what I wanted it to do, I checked it thoroughly. though you are right, I didn't see the subscript error coming when I posted my solution before. but that was quite easy to solve: Vector Status_Sem_= Status_Sem_1 to Status_Sem_14. Loop #i=1 to 13. Do If Status_Sem_(#i) ='Z'. Loop #y=1 to 14-#i. Do If Status_Sem_(#y+#i) ='Z' OR Status_Sem_(#y+#i) ='I' OR Status_Sem_(#y+#i) ='G' OR Status_Sem_(#y+#i) ='X' OR Status_Sem_(#y+#i) ='P' OR Status_Sem_(#y+#i) ='U'. Compute Status_Sem_(#i)='P'. End if. end loop. end if. end loop. On Tue, 13 May 2014 08:15:44 -0700, David Marso <[hidden email]> wrote: >Pretty sure your code does not do what you describe you want and will give >tons of warnings about out of range subscripts. > >Other than that, maybe you should post a snapshot of before and after of a >few cases of data? > >Rather than > >Do If Status_Sem_(#y+#i) ='Z' OR Status_Sem_(#y+#i) ='I' OR >Status_Sem_(#y+#i) ='G' OR Status_Sem_(#y+#i) ='X' OR Status_Sem_(#y+#i) >='P' OR Status_Sem_(#y+#i) ='U'. >Compute Status_Sem_(#i)='P'. >End if. > >Consider >IF ANY(Status_Sem_(#y+#i),"Z","I","G","X","P","U") Status_Sem_(#i)='P'. >Alternatively >IF INDEX(Status_Sem_(#y+#i),"ZIGXPU",1) GT 0 Status_Sem_(#i)='P'. > > > > > >----- >Please reply to the list and not to my personal email. >Those desiring my consulting or training services please feel free to email >--- >"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?" >-- >View this message in context: http://spssx- discussion.1045642.n5.nabble.com/Loop-through-variable-names- tp5725986p5726009.html >Sent from the SPSSX Discussion mailing list archive at Nabble.com. > >===================== >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 |
At 12:45 PM 5/13/2014, Julian wrote:
>Vector Status_Sem_= Status_Sem_1 to Status_Sem_14. >Loop #i=1 to 13. >. Do If Status_Sem_(#i) ='Z'. >. Loop #y=1 to 14-#i. >. Do If Status_Sem_(#y+#i) ='Z' > OR Status_Sem_(#y+#i) ='I' > OR Status_Sem_(#y+#i) ='G' > OR Status_Sem_(#y+#i) ='X' > OR Status_Sem_(#y+#i) ='P' > OR Status_Sem_(#y+#i) ='U'. >. Compute Status_Sem_(#i)='P'. >. End if. >. end loop. >. end if. >end loop. I've reformatted, above, for readability -- the original was very difficult. There look to be a couple of important simplifications possible -- code not tested: Vector Status_Sem_= Status_Sem_1 to Status_Sem_14. Loop #i=1 to 13. . Do If Status_Sem_(#i) ='Z'. . Loop #y=#i+1 to 14. . If ANY(Status_Sem_(#y),'Z','I','G','X','P','U') . Status_Sem_(#i)='P'. . end loop. . end if. end loop. ===================== 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 |