|
Hey all,
I'm having a slight problem with some of my DO IF statements. I'm not getting any error messages in the log but some of the ".. ELSE IF.. " statements do not seem to run. For example, in the code below, the first two IF statements run fine but the final one doesn't run. At then end, the values I get for c are '1', '3' and missings but no '2'. DO IF a=1. COMPUTE c=3. ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). COMPUTE c=1. ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). COMPUTE expt1u=2. END IF. EXECUTE. However if I take that final statement and paste it in a new DO IF statement with only those criteria, it works just fine. The code looks fine. The criteria are legit (I've selected cases based on those criteria and they do exist). Is there something else I'm missing here? I initially noticed the issue a couple of weeks back but and initially it was little inconvenient to create new DO IF statements for the lines that didn't run, but now it's just getting annoying. Not to mention that I worry that there might be something worse going on that I'm not aware of. Incidentaly, all this is being run in SPSS 15.0 on an XP machine. Thanks a heap in advance. |
|
I recently had a similar experience (with SPSS 14) and a colleague
recommended running the else statements again separately due to experiencing a similar phenomenon. Re-running worked fine that way. I too am curious about why the statements did not appear to work in the DO IF..ELSE IF' loop. Melissa -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Syed Hashmi Sent: Wednesday, August 08, 2007 3:02 PM To: [hidden email] Subject: [SPSSX-L] DO IF.. ELSE IF statements Hey all, I'm having a slight problem with some of my DO IF statements. I'm not getting any error messages in the log but some of the ".. ELSE IF.. " statements do not seem to run. For example, in the code below, the first two IF statements run fine but the final one doesn't run. At then end, the values I get for c are '1', '3' and missings but no '2'. DO IF a=1. COMPUTE c=3. ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). COMPUTE c=1. ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). COMPUTE expt1u=2. END IF. EXECUTE. However if I take that final statement and paste it in a new DO IF statement with only those criteria, it works just fine. The code looks fine. The criteria are legit (I've selected cases based on those criteria and they do exist). Is there something else I'm missing here? I initially noticed the issue a couple of weeks back but and initially it was little inconvenient to create new DO IF statements for the lines that didn't run, but now it's just getting annoying. Not to mention that I worry that there might be something worse going on that I'm not aware of. Incidentaly, all this is being run in SPSS 15.0 on an XP machine. Thanks a heap in advance. PRIVILEGED AND CONFIDENTIAL INFORMATION This transmittal and any attachments may contain PRIVILEGED AND CONFIDENTIAL information and is intended only for the use of the addressee. If you are not the designated recipient, or an employee or agent authorized to deliver such transmittals to the designated recipient, you are hereby notified that any dissemination, copying or publication of this transmittal is strictly prohibited. If you have received this transmittal in error, please notify us immediately by replying to the sender and delete this copy from your system. You may also call us at (309) 827-6026 for assistance. |
|
Same here. They work fine if run separately. I've done just that and
modified my code but it would be good to know why it happens. Shahrukh. > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of > Melissa Ives > Sent: Wednesday, August 08, 2007 3:06 PM > To: [hidden email] > Subject: Re: DO IF.. ELSE IF statements > > I recently had a similar experience (with SPSS 14) and a colleague > recommended running the else statements again separately due to > experiencing a similar phenomenon. Re-running worked fine that way. I > too am curious about why the statements did not appear to work in the DO > IF..ELSE IF' loop. > > Melissa > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of > Syed Hashmi > Sent: Wednesday, August 08, 2007 3:02 PM > To: [hidden email] > Subject: [SPSSX-L] DO IF.. ELSE IF statements > > Hey all, > > I'm having a slight problem with some of my DO IF statements. I'm not > getting any error messages in the log but some of the ".. ELSE IF.. " > statements do not seem to run. > > For example, in the code below, the first two IF statements run fine > the final one doesn't run. At then end, the values I get for c are '1', > '3' and missings but no '2'. > > DO IF a=1. > COMPUTE c=3. > ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). > COMPUTE c=1. > ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). > COMPUTE expt1u=2. > END IF. > EXECUTE. > > However if I take that final statement and paste it in a new DO IF > statement with only those criteria, it works just fine. The code looks > fine. The criteria are legit (I've selected cases based on those > criteria and they do exist). Is there something else I'm missing > I initially noticed the issue a couple of weeks back but and initially > it was little inconvenient to create new DO IF statements for the lines > that didn't run, but now it's just getting annoying. Not to mention that > I worry that there might be something worse going on that I'm not aware > of. > > Incidentaly, all this is being run in SPSS 15.0 on an XP machine. > > Thanks a heap in advance. > > > PRIVILEGED AND CONFIDENTIAL INFORMATION > This transmittal and any attachments may contain PRIVILEGED AND > CONFIDENTIAL information and is intended only for the use of the > addressee. If you are not the designated recipient, or an employee > or agent authorized to deliver such transmittals to the designated > recipient, you are hereby notified that any dissemination, > copying or publication of this transmittal is strictly prohibited. If > you have received this transmittal in error, please notify us > immediately by replying to the sender and delete this copy from your > system. You may also call us at (309) 827-6026 for assistance. |
|
In reply to this post by Hashmi, Syed S
>some of the ".. ELSE IF.. "
**
>statements do not seem to run. > >For example, in the code below, the first two IF statements run >fine but the final one doesn't run. At then end, the values I get >for **c** are '1', '3' and missings but no '2'. > > DO IF a=1. > COMPUTE c=3. > ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). > COMPUTE c=1. > ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). > COMPUTE expt1u=2. ** > END IF. > EXECUTE. (** = emphasis added) I'm reluctant to comment with so many list-members who are much more SPSS-adept than I am. Still, I notice that your question asks about missing "2" values for "c", but your *syntax* assigns the value of "2" to "exp1u" (and *not* to "c"). Gary --- Gary Rosin <[hidden email]> "You're just a wave, you're not the water." --Butch Hancock |
|
It's because everywhere an a = 1 is already dealt with in the first
block - all combinations of a = 1 won't be addressed afterwards (because they already have been!). If you do them seperately, be SURE you want the results it gives you....some initial computations will be overwritten! -Gary On 8/8/07, Gary Rosin <[hidden email]> wrote: > >some of the ".. ELSE IF.. " > >statements do not seem to run. > > > >For example, in the code below, the first two IF statements run > >fine but the final one doesn't run. At then end, the values I get > >for **c** are '1', '3' and missings but no '2'. > > > > DO IF a=1. > > COMPUTE c=3. > > ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). > > COMPUTE c=1. > > ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). > ** > > COMPUTE expt1u=2. > ** > > END IF. > > EXECUTE. > > (** = emphasis added) > > I'm reluctant to comment with so many list-members who are much > more SPSS-adept than I am. Still, I notice that your question asks > about missing "2" values for "c", but your *syntax* assigns the > value of "2" to "exp1u" (and *not* to "c"). > > Gary > > --- > Gary Rosin <[hidden email]> > > "You're just a wave, > you're not the water." > --Butch Hancock > |
|
In reply to this post by Hashmi, Syed S
Once a condition is met for a case, processing stops for that case, and the DO IF structure moves on to the next case. So for every case where a=1, the first condition is met, c is set to 3, and DO IF moves on to the next case. Since the remaining conditions include a=1, they're never applied to any cases, because any case where a=1 has already met the first condition.
-----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Hashmi, Syed S Sent: Wednesday, August 08, 2007 3:24 PM To: [hidden email] Subject: Re: DO IF.. ELSE IF statements Same here. They work fine if run separately. I've done just that and modified my code but it would be good to know why it happens. Shahrukh. > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of > Melissa Ives > Sent: Wednesday, August 08, 2007 3:06 PM > To: [hidden email] > Subject: Re: DO IF.. ELSE IF statements > > I recently had a similar experience (with SPSS 14) and a colleague > recommended running the else statements again separately due to > experiencing a similar phenomenon. Re-running worked fine that way. I > too am curious about why the statements did not appear to work in the DO > IF..ELSE IF' loop. > > Melissa > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of > Syed Hashmi > Sent: Wednesday, August 08, 2007 3:02 PM > To: [hidden email] > Subject: [SPSSX-L] DO IF.. ELSE IF statements > > Hey all, > > I'm having a slight problem with some of my DO IF statements. I'm not > getting any error messages in the log but some of the ".. ELSE IF.. " > statements do not seem to run. > > For example, in the code below, the first two IF statements run fine > the final one doesn't run. At then end, the values I get for c are '1', > '3' and missings but no '2'. > > DO IF a=1. > COMPUTE c=3. > ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). > COMPUTE c=1. > ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). > COMPUTE expt1u=2. > END IF. > EXECUTE. > > However if I take that final statement and paste it in a new DO IF > statement with only those criteria, it works just fine. The code looks > fine. The criteria are legit (I've selected cases based on those > criteria and they do exist). Is there something else I'm missing > I initially noticed the issue a couple of weeks back but and initially > it was little inconvenient to create new DO IF statements for the lines > that didn't run, but now it's just getting annoying. Not to mention that > I worry that there might be something worse going on that I'm not aware > of. > > Incidentaly, all this is being run in SPSS 15.0 on an XP machine. > > Thanks a heap in advance. > > > PRIVILEGED AND CONFIDENTIAL INFORMATION > This transmittal and any attachments may contain PRIVILEGED AND > CONFIDENTIAL information and is intended only for the use of the > addressee. If you are not the designated recipient, or an employee > or agent authorized to deliver such transmittals to the designated > recipient, you are hereby notified that any dissemination, > copying or publication of this transmittal is strictly prohibited. If > you have received this transmittal in error, please notify us > immediately by replying to the sender and delete this copy from your > system. You may also call us at (309) 827-6026 for assistance. |
|
In reply to this post by Gary Rosin
Yes, sorry about that. I had renamed my variables for the post and had
forgotten to change it in that last line. My bad! But c = expt1u = 2. Shahrukh > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of > Gary Rosin > Sent: Wednesday, August 08, 2007 3:59 PM > To: [hidden email] > Subject: Re: DO IF.. ELSE IF statements > > >some of the ".. ELSE IF.. " > >statements do not seem to run. > > > >For example, in the code below, the first two IF statements run > >fine but the final one doesn't run. At then end, the values I get > >for **c** are '1', '3' and missings but no '2'. > > > > DO IF a=1. > > COMPUTE c=3. > > ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). > > COMPUTE c=1. > > ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). > ** > > COMPUTE expt1u=2. > ** > > END IF. > > EXECUTE. > > (** = emphasis added) > > I'm reluctant to comment with so many list-members who are much > more SPSS-adept than I am. Still, I notice that your question asks > about missing "2" values for "c", but your *syntax* assigns the > value of "2" to "exp1u" (and *not* to "c"). > > Gary > > --- > Gary Rosin <[hidden email]> > > "You're just a wave, > you're not the water." > --Butch Hancock |
|
In reply to this post by Oliver, Richard
So you're saying SPSS will look at the first of perhaps numerous
criteria and if that one has already been satisfied in a previous ..IF.. statement then it'll stop processing any more ..ELSE IF.. statements in the loop? Does this mean that SPSS doesn't look at the other criteria in that IF statement? What if all the criteria are enclosed in ( )?? Furthermore, I have run a bunch of DO IF..ELSE IF loops where the first criteria for all of them was the same and there followed about 6 more criteria that were different in each ELSE IF statement. Many of these loops worked perfectly. Of some that didn't work perfectly, some ELSE IF statements did, some didn't, although the syntax was the same all through. - Shahrukh > -----Original Message----- > From: Oliver, Richard [mailto:[hidden email]] > Sent: Wednesday, August 08, 2007 4:14 PM > To: Hashmi, Syed S; [hidden email] > Subject: RE: Re: DO IF.. ELSE IF statements > > Once a condition is met for a case, processing stops for that case, and > the DO IF structure moves on to the next case. So for every case where > a=1, the first condition is met, c is set to 3, and DO IF moves on to the > next case. Since the remaining conditions include a=1, they're never > applied to any cases, because any case where a=1 has already met the first > condition. > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of > Hashmi, Syed S > Sent: Wednesday, August 08, 2007 3:24 PM > To: [hidden email] > Subject: Re: DO IF.. ELSE IF statements > > Same here. They work fine if run separately. I've done just that and > modified my code but it would be good to know why it happens. > > Shahrukh. > > > > -----Original Message----- > > From: SPSSX(r) Discussion [mailto:[hidden email]] On > Of > > Melissa Ives > > Sent: Wednesday, August 08, 2007 3:06 PM > > To: [hidden email] > > Subject: Re: DO IF.. ELSE IF statements > > > > I recently had a similar experience (with SPSS 14) and a colleague > > recommended running the else statements again separately due to > > experiencing a similar phenomenon. Re-running worked fine that way. > I > > too am curious about why the statements did not appear to work in > DO > > IF..ELSE IF' loop. > > > > Melissa > > > > -----Original Message----- > > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf > Of > > Syed Hashmi > > Sent: Wednesday, August 08, 2007 3:02 PM > > To: [hidden email] > > Subject: [SPSSX-L] DO IF.. ELSE IF statements > > > > Hey all, > > > > I'm having a slight problem with some of my DO IF statements. I'm not > > getting any error messages in the log but some of the ".. ELSE IF.. " > > statements do not seem to run. > > > > For example, in the code below, the first two IF statements run fine > but > > the final one doesn't run. At then end, the values I get for c are > '1', > > '3' and missings but no '2'. > > > > DO IF a=1. > > COMPUTE c=3. > > ELSE IF (a=1 & (b1=1 OR b2=1 OR b3=1)). > > COMPUTE c=1. > > ELSE IF (a=1 & (b1=2 & b2=2 & b3=2)). > > COMPUTE expt1u=2. > > END IF. > > EXECUTE. > > > > However if I take that final statement and paste it in a new DO IF > > statement with only those criteria, it works just fine. The code > > fine. The criteria are legit (I've selected cases based on those > > criteria and they do exist). Is there something else I'm missing > here? > > I initially noticed the issue a couple of weeks back but and initially > > it was little inconvenient to create new DO IF statements for the > lines > > that didn't run, but now it's just getting annoying. Not to mention > that > > I worry that there might be something worse going on that I'm not > aware > > of. > > > > Incidentaly, all this is being run in SPSS 15.0 on an XP machine. > > > > Thanks a heap in advance. > > > > > > PRIVILEGED AND CONFIDENTIAL INFORMATION > > This transmittal and any attachments may contain PRIVILEGED AND > > CONFIDENTIAL information and is intended only for the use of the > > addressee. If you are not the designated recipient, or an employee > > or agent authorized to deliver such transmittals to the designated > > recipient, you are hereby notified that any dissemination, > > copying or publication of this transmittal is strictly prohibited. > > you have received this transmittal in error, please notify us > > immediately by replying to the sender and delete this copy from your > > system. You may also call us at (309) 827-6026 for assistance. |
|
In reply to this post by Hashmi, Syed S
On Wed, 8 Aug 2007 14:04:26 -0700, Hal 9000 <[hidden email]> wrote:
>It's because everywhere an a = 1 is already dealt with in the first >block - all combinations of a = 1 won't be addressed afterwards >(because they already have been!). If you do them seperately, be SURE >you want the results it gives you....some initial computations will be >overwritten! >-Gary > Gary, I just posted another reply to Richard's post which dealth with the same issue of "has the first criterion been satisfied?". Like I mentioned in that post, there is probably some other reason why that loop doesn't work since there are other loops with similar first criterion that worked just fine. Also, am I wrong in assuming that DO IF loops with criteria of DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) would be more or less commonplace? Shouldn't the criteria be looked at in full as opposed with >1 sub-criterion? I'm just curious about this since the first criterion rule doesn't seem to apply everytime. - Shahrukh |
|
In reply to this post by Hashmi, Syed S
In the case you mention below, I would do it...
DO IF a=1. do if (b=1). <commands> else if (b=2). <commands> end if. ELSE IF a=2. <etc.> END IF. Melissa -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Syed Hashmi Sent: Wednesday, August 08, 2007 4:44 PM To: [hidden email] Subject: Re: [SPSSX-L] DO IF.. ELSE IF statements On Wed, 8 Aug 2007 14:04:26 -0700, Hal 9000 <[hidden email]> wrote: >It's because everywhere an a = 1 is already dealt with in the first >block - all combinations of a = 1 won't be addressed afterwards >(because they already have been!). If you do them seperately, be SURE >you want the results it gives you....some initial computations will be >overwritten! >-Gary > Gary, I just posted another reply to Richard's post which dealth with the same issue of "has the first criterion been satisfied?". Like I mentioned in that post, there is probably some other reason why that loop doesn't work since there are other loops with similar first criterion that worked just fine. Also, am I wrong in assuming that DO IF loops with criteria of DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) would be more or less commonplace? Shouldn't the criteria be looked at in full as opposed with >1 sub-criterion? I'm just curious about this since the first criterion rule doesn't seem to apply everytime. - Shahrukh PRIVILEGED AND CONFIDENTIAL INFORMATION This transmittal and any attachments may contain PRIVILEGED AND CONFIDENTIAL information and is intended only for the use of the addressee. If you are not the designated recipient, or an employee or agent authorized to deliver such transmittals to the designated recipient, you are hereby notified that any dissemination, copying or publication of this transmittal is strictly prohibited. If you have received this transmittal in error, please notify us immediately by replying to the sender and delete this copy from your system. You may also call us at (309) 827-6026 for assistance. |
|
In reply to this post by Hashmi, Syed S
> Also, am I wrong in assuming that DO IF loops with criteria of
> DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) Those are not mutually exclusive distinctions. You have to specify do if a=1 & b=1 & not (such and such condition). else if a=1 & b=1 & (such & such condition). a silly example: Do if (I like apple pie). compute X = 1. Do if(I like apple pie and blueberry pie). compute X = 2. end if. For that row of data, it stops after the first 'do if' because it finds a match. It doesn't keep looking past the fact that you like apple pie, because you are telling the program these categories are mutually exclusive. This is OK: do if (I like apple pie and DON'T like blueberry pie). compute X = 1. do if (I like apple pie and DO like blueberry pie). compute X = 2. end if. Melissa's post is nice, but it's really important to understand the logic rather than find something that works (or appears to work, but actually doesn't). |
|
Those are not mutually exclusive distinctions
Ooops, they are! Haha On 8/8/07, Hal 9000 <[hidden email]> wrote: > > Also, am I wrong in assuming that DO IF loops with criteria of > > DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) > > Those are not mutually exclusive distinctions. You have to specify > > do if a=1 & b=1 & not (such and such condition). > else if a=1 & b=1 & (such & such condition). > > a silly example: > > Do if (I like apple pie). > compute X = 1. > Do if(I like apple pie and blueberry pie). > compute X = 2. > end if. > > For that row of data, it stops after the first 'do if' because it > finds a match. It doesn't keep looking past the fact that you like > apple pie, because you are telling the program these categories are > mutually exclusive. > > This is OK: > > do if (I like apple pie and DON'T like blueberry pie). > compute X = 1. > do if (I like apple pie and DO like blueberry pie). > compute X = 2. > end if. > > Melissa's post is nice, but it's really important to understand the > logic rather than find something that works (or appears to work, but > actually doesn't). > |
|
In reply to this post by Hashmi, Syed S
This is OK as well:
do if (I like apple pie) . compute X = 1. do if (I like blueberry pie). compute X = 2. end if. end if. Because it first searches for any liking of apple pie, then once those are identified (with X=1) looks at that list for who also likes blueberry and changes X to 2 in the final calculation -- only because the blueberry DO IF is embedded within the apple DO IF. Of course, neither syntax does anything if you only like blueberry pie and not apple or if you prefer lemon over either/both of these :-) (I love all 3!) Melissa (BTW-Hal, you need a second END IF somewhere--where often makes a difference.) -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Hal 9000 Sent: Wednesday, August 08, 2007 5:01 PM To: [hidden email] Subject: Re: [SPSSX-L] DO IF.. ELSE IF statements > Also, am I wrong in assuming that DO IF loops with criteria of > DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) Those are not mutually exclusive distinctions. You have to specify do if a=1 & b=1 & not (such and such condition). else if a=1 & b=1 & (such & such condition). a silly example: Do if (I like apple pie). compute X = 1. Do if(I like apple pie and blueberry pie). compute X = 2. end if. For that row of data, it stops after the first 'do if' because it finds a match. It doesn't keep looking past the fact that you like apple pie, because you are telling the program these categories are mutually exclusive. This is OK: do if (I like apple pie and DON'T like blueberry pie). compute X = 1. do if (I like apple pie and DO like blueberry pie). compute X = 2. end if. Melissa's post is nice, but it's really important to understand the logic rather than find something that works (or appears to work, but actually doesn't). PRIVILEGED AND CONFIDENTIAL INFORMATION This transmittal and any attachments may contain PRIVILEGED AND CONFIDENTIAL information and is intended only for the use of the addressee. If you are not the designated recipient, or an employee or agent authorized to deliver such transmittals to the designated recipient, you are hereby notified that any dissemination, copying or publication of this transmittal is strictly prohibited. If you have received this transmittal in error, please notify us immediately by replying to the sender and delete this copy from your system. You may also call us at (309) 827-6026 for assistance. |
|
In reply to this post by Hashmi, Syed S
You need to post examples of the "loops with similar first criterion that worked just fine", because they may not be doing what you think they are doing.
You should also read the DO IF SPSS syntax documentation, and then for comparison look at documentation of control structures in C, Python, Fortran, Java, etc... How ELSE IF functions in SPSS is how the comparable control structures work in other programming languages. Melissa's suggestion is a very sensible one. Alex -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Syed Hashmi Sent: Wednesday, August 08, 2007 4:44 PM To: [hidden email] Subject: Re: DO IF.. ELSE IF statements On Wed, 8 Aug 2007 14:04:26 -0700, Hal 9000 <[hidden email]> wrote: >It's because everywhere an a = 1 is already dealt with in the first >block - all combinations of a = 1 won't be addressed afterwards >(because they already have been!). If you do them seperately, be SURE >you want the results it gives you....some initial computations will be >overwritten! >-Gary > Gary, I just posted another reply to Richard's post which dealth with the same issue of "has the first criterion been satisfied?". Like I mentioned in that post, there is probably some other reason why that loop doesn't work since there are other loops with similar first criterion that worked just fine. Also, am I wrong in assuming that DO IF loops with criteria of DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) would be more or less commonplace? Shouldn't the criteria be looked at in full as opposed with >1 sub-criterion? I'm just curious about this since the first criterion rule doesn't seem to apply everytime. - Shahrukh |
|
In reply to this post by Hal 9000
Thanks Hal. (and Melissa and Gary).
That made more sense. I just assumed that it would look at the condition in entirety as opposed to breaking it down to each single criterion in the condition. For example, in your example it would compute X=1 for all those who like apple pie and then compute X=2 for the subset of the same apple pie 'likers' who ALSO like blueberry pie - but like you mentioned, the two statements are not mutually exclusive. Thanks for the explanation. I've already recoded all my work but this info will probably come in handy the next time round. - Shahrukh > -----Original Message----- > From: Hal 9000 [mailto:[hidden email]] > Sent: Wednesday, August 08, 2007 5:01 PM > To: Hashmi, Syed S; [hidden email] > Subject: Re: DO IF.. ELSE IF statements > > > Also, am I wrong in assuming that DO IF loops with criteria of > > DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) > > Those are not mutually exclusive distinctions. You have to specify > > do if a=1 & b=1 & not (such and such condition). > else if a=1 & b=1 & (such & such condition). > > a silly example: > > Do if (I like apple pie). > compute X = 1. > Do if(I like apple pie and blueberry pie). > compute X = 2. > end if. > > For that row of data, it stops after the first 'do if' because it > finds a match. It doesn't keep looking past the fact that you like > apple pie, because you are telling the program these categories are > mutually exclusive. > > This is OK: > > do if (I like apple pie and DON'T like blueberry pie). > compute X = 1. > do if (I like apple pie and DO like blueberry pie). > compute X = 2. > end if. > > Melissa's post is nice, but it's really important to understand the > logic rather than find something that works (or appears to work, but > actually doesn't). |
|
In reply to this post by Hashmi, Syed S
On Wed, 8 Aug 2007 17:50:34 -0500, Reutter, Alex <[hidden email]> wrote:
>You need to post examples of the "loops with similar first criterion that worked just fine", because they may not be doing what you think they are doing. > >You should also read the DO IF SPSS syntax documentation, and then for comparison look at documentation of control structures in C, Python, Fortran, Java, etc... How ELSE IF functions in SPSS is how the comparable control structures work in other programming languages. > >Melissa's suggestion is a very sensible one. > >Alex > Alex, Here's a bit of code that worked fine completely. The whole loop was within a DO REPEAT command and 'a' to 'g' were different variables defined in the DO REPEAT command. If you notice, the first four condition in each IF statement are the same: DO IF respill=1 /* code 4.51 */ & NOT(MISSING(a)) & b GE 1 & b LE 12 & MISSING(c) & NOT(MISSING(d)) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 4.51. ELSE IF respill=1 /* code 4.52 */ & NOT(MISSING(a)) & b GE 1 & b LE 12 & MISSING(c) & MISSING(d) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 4.52. END IF. On the other hand, here's a bit of code that didn't work completely. Codes 3.11, 3.13 and 3.15 worked, but 3.12 and 3.14 didn't. I can see how they're different, but the firt three conditions in each IF statement are again same. They are all mutually exclusive only if you take the whole IF statement in it's entirety but not if you break it down by sub-conditions. It's the same as the code that worked above. DO IF respill=1 /* code 3.11 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & c GE 91 & c LE 93 & NOT(MISSING(d)) & e GE 1 & e LE 12 & f GE 1 & f LE 31 & MISSING(g). COMPUTE x = 3.11. ELSE IF respill=1 /* code 3.12 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & MISSING(c) & NOT(MISSING(d)) & e GE 1 & e LE 12 & f GE 1 & f LE 31 & MISSING(g). COMPUTE x = 3.12. ELSE IF respill=1 /* code 3.13 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & MISSING(c) & NOT(MISSING(d)) & e GE 1 & e LE 12 & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 3.13. ELSE IF respill=1 /* code 3.14 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & MISSING(c) & NOT(MISSING(d)) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 3.14. ELSE IF respill=1 /* code 3.15 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & c GE 1 & c LE 31 & MISSING(d) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 3.15. END IF. I'll go over the documentation that you had suggested. I had tried similar code in SAS and it had worked but haven't tried in any of the other languages. Thanks again for the tip. - Shahrukh |
|
In reply to this post by Hashmi, Syed S
DO IF-ELSE IF evaluates each condition separately. They don't have to be mutually exclusive conditions, although that may make your code easier to debug. It all depends on what you're trying to accomplish. Sometimes you may want to go from the most restrictive condition to least restrictive, sometimes the reverse. In your original example, you started with the least restrictive condition, causing any cases that met any of your conditions to meet the first condition.
-----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Hashmi, Syed S Sent: Wednesday, August 08, 2007 5:58 PM To: [hidden email] Subject: Re: DO IF.. ELSE IF statements Thanks Hal. (and Melissa and Gary). That made more sense. I just assumed that it would look at the condition in entirety as opposed to breaking it down to each single criterion in the condition. For example, in your example it would compute X=1 for all those who like apple pie and then compute X=2 for the subset of the same apple pie 'likers' who ALSO like blueberry pie - but like you mentioned, the two statements are not mutually exclusive. Thanks for the explanation. I've already recoded all my work but this info will probably come in handy the next time round. - Shahrukh > -----Original Message----- > From: Hal 9000 [mailto:[hidden email]] > Sent: Wednesday, August 08, 2007 5:01 PM > To: Hashmi, Syed S; [hidden email] > Subject: Re: DO IF.. ELSE IF statements > > > Also, am I wrong in assuming that DO IF loops with criteria of > > DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) > > Those are not mutually exclusive distinctions. You have to specify > > do if a=1 & b=1 & not (such and such condition). > else if a=1 & b=1 & (such & such condition). > > a silly example: > > Do if (I like apple pie). > compute X = 1. > Do if(I like apple pie and blueberry pie). > compute X = 2. > end if. > > For that row of data, it stops after the first 'do if' because it > finds a match. It doesn't keep looking past the fact that you like > apple pie, because you are telling the program these categories are > mutually exclusive. > > This is OK: > > do if (I like apple pie and DON'T like blueberry pie). > compute X = 1. > do if (I like apple pie and DO like blueberry pie). > compute X = 2. > end if. > > Melissa's post is nice, but it's really important to understand the > logic rather than find something that works (or appears to work, but > actually doesn't). |
|
Is this thread making anybody else hungry?
Hum, good point - I'm so conditioned to covering ALL of my bases using clearly delimited and mutually exclusive distinctions that the possibility of layered processing with decreasingly strict conditions didn't occur to me! For moderately complex combinations it could be a debugging nightmare in the making, though (for my mind, at least). Melissa - your code makes/made perfect sense, and even looks pretty. I only meant that in this case it seems more important to demysify why it works or doesn't work. I certainly refined my own way of thinking about this flow control method, so I won't apologize for bumbling around in the thread! :) [note to self : do not multitask] On 8/8/07, Oliver, Richard <[hidden email]> wrote: > DO IF-ELSE IF evaluates each condition separately. They don't have to be mutually exclusive conditions, although that may make your code easier to debug. It all depends on what you're trying to accomplish. Sometimes you may want to go from the most restrictive condition to least restrictive, sometimes the reverse. In your original example, you started with the least restrictive condition, causing any cases that met any of your conditions to meet the first condition. > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Hashmi, Syed S > Sent: Wednesday, August 08, 2007 5:58 PM > To: [hidden email] > Subject: Re: DO IF.. ELSE IF statements > > Thanks Hal. (and Melissa and Gary). > > That made more sense. I just assumed that it would look at the condition > in entirety as opposed to breaking it down to each single criterion in > the condition. For example, in your example it would compute X=1 for all > those who like apple pie and then compute X=2 for the subset of the same > apple pie 'likers' who ALSO like blueberry pie - but like you mentioned, > the two statements are not mutually exclusive. > > Thanks for the explanation. I've already recoded all my work but this > info will probably come in handy the next time round. > > - Shahrukh > > > -----Original Message----- > > From: Hal 9000 [mailto:[hidden email]] > > Sent: Wednesday, August 08, 2007 5:01 PM > > To: Hashmi, Syed S; [hidden email] > > Subject: Re: DO IF.. ELSE IF statements > > > > > Also, am I wrong in assuming that DO IF loops with criteria of > > > DO IF (a=1 & b=1)... ELSE IF (a=1 & b=2) > > > > Those are not mutually exclusive distinctions. You have to specify > > > > do if a=1 & b=1 & not (such and such condition). > > else if a=1 & b=1 & (such & such condition). > > > > a silly example: > > > > Do if (I like apple pie). > > compute X = 1. > > Do if(I like apple pie and blueberry pie). > > compute X = 2. > > end if. > > > > For that row of data, it stops after the first 'do if' because it > > finds a match. It doesn't keep looking past the fact that you like > > apple pie, because you are telling the program these categories are > > mutually exclusive. > > > > This is OK: > > > > do if (I like apple pie and DON'T like blueberry pie). > > compute X = 1. > > do if (I like apple pie and DO like blueberry pie). > > compute X = 2. > > end if. > > > > Melissa's post is nice, but it's really important to understand the > > logic rather than find something that works (or appears to work, but > > actually doesn't). > |
|
In reply to this post by Hashmi, Syed S
The example that works below does not have similar criteria to the one that doesn't. In the first example, the set of cases for which the DO IF (code 4.51) evaluates to true is mutually exclusive with the set of cases for which the ELSE IF (code 4.52) evaluates to true; to simplify, the expression for 4.51 is essentially (this & NOT(that))** and the expression for 4.52 is (this & that).
In the second example, the set of cases for which the first ELSE IF (code 3.12) evaluates to true is a subset of the set of cases for which the DO IF (code 3.11) evaluates to true; to simplify, the expression for 3.11 is essentially (this) and the expression for 3.12 is (this & that)***. Add & NOT(MISSING(c)) to the DO IF (code 3.11) and 3.12 should "work". It's all a matter of set logic. Does this help? That is, do you see what needs to be done to make 3.14 work? Cheers, Alex ** where "this" is respill=1 & NOT(MISSING(a)) & b GE 1 & b LE 12 & MISSING(c) & MISSING(e) & MISSING(f) & NOT(MISSING(g)) and "that" is MISSING(d) *** where "this" is respill=1 & NOT(MISSING(a)) & b GE 21 & b LE 33 & c GE 91 & c LE 93 & NOT(MISSING(d)) & e GE 1 & e LE 12 & f GE 1 & f LE 31 & MISSING(g) and "that" is MISSING(c) -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Syed Hashmi Sent: Wednesday, August 08, 2007 6:52 PM To: [hidden email] Subject: Re: DO IF.. ELSE IF statements On Wed, 8 Aug 2007 17:50:34 -0500, Reutter, Alex <[hidden email]> wrote: >You need to post examples of the "loops with similar first criterion that worked just fine", because they may not be doing what you think they are doing. > >You should also read the DO IF SPSS syntax documentation, and then for comparison look at documentation of control structures in C, Python, Fortran, Java, etc... How ELSE IF functions in SPSS is how the comparable control structures work in other programming languages. > >Melissa's suggestion is a very sensible one. > >Alex > Alex, Here's a bit of code that worked fine completely. The whole loop was within a DO REPEAT command and 'a' to 'g' were different variables defined in the DO REPEAT command. If you notice, the first four condition in each IF statement are the same: DO IF respill=1 /* code 4.51 */ & NOT(MISSING(a)) & b GE 1 & b LE 12 & MISSING(c) & NOT(MISSING(d)) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 4.51. ELSE IF respill=1 /* code 4.52 */ & NOT(MISSING(a)) & b GE 1 & b LE 12 & MISSING(c) & MISSING(d) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 4.52. END IF. On the other hand, here's a bit of code that didn't work completely. Codes 3.11, 3.13 and 3.15 worked, but 3.12 and 3.14 didn't. I can see how they're different, but the firt three conditions in each IF statement are again same. They are all mutually exclusive only if you take the whole IF statement in it's entirety but not if you break it down by sub-conditions. It's the same as the code that worked above. DO IF respill=1 /* code 3.11 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & c GE 91 & c LE 93 & NOT(MISSING(d)) & e GE 1 & e LE 12 & f GE 1 & f LE 31 & MISSING(g). COMPUTE x = 3.11. ELSE IF respill=1 /* code 3.12 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & MISSING(c) & NOT(MISSING(d)) & e GE 1 & e LE 12 & f GE 1 & f LE 31 & MISSING(g). COMPUTE x = 3.12. ELSE IF respill=1 /* code 3.13 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & MISSING(c) & NOT(MISSING(d)) & e GE 1 & e LE 12 & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 3.13. ELSE IF respill=1 /* code 3.14 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & MISSING(c) & NOT(MISSING(d)) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 3.14. ELSE IF respill=1 /* code 3.15 */ & NOT(MISSING(a)) & b GE 21 & b LE 33 & c GE 1 & c LE 31 & MISSING(d) & MISSING(e) & MISSING(f) & NOT(MISSING(g)). COMPUTE x = 3.15. END IF. I'll go over the documentation that you had suggested. I had tried similar code in SAS and it had worked but haven't tried in any of the other languages. Thanks again for the tip. - Shahrukh |
|
In reply to this post by Hal 9000
Thanks to Melissa, Richard, Gary, Alex and anybody else who replied to
my DO IF...ELSE IF query. It's a lot clearer to me now and I might have to either nest the statements like Melissa did or just go from least restrictive to most restrictive. I'll have to keep all this in mind for the next time I code. Thanks again all!! - Shahrukh |
| Free forum by Nabble | Edit this page |
