Dear Friends, I’m trying to combine following DO IF, IF, and DO IF into one DO IF. I don’t want to recode sysmis into another value. When I’ve combined the codes under a single DO IF, and run the test data, I get only groups 1-4 and the rest missing. Any help will be greatly appreciated. TIA Stephen Salbod, Pace University, NYC DATA LIST / Practitioner 1 StudentStat 2 . BEGIN DATA 11 12 21 22 1 2 1 2 END DATA. DO IF (Practitioner EQ 1 AND StudentStat EQ 1). COMPUTE group = 1. ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). COMPUTE group = 2. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). COMPUTE group = 3. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). COMPUTE group = 4. END IF. IF (MISSING(Practitioner) AND MISSING(StudentStat)) group = 5. DO IF (MISSING(Practitioner)). + DO IF (StudentStat EQ 1). + COMPUTE group = 6. + ELSE IF (StudentStat EQ 2). + COMPUTE group = 7. + END IF. ELSE IF (MISSING(StudentStat)). + DO IF (Practitioner EQ 1). + COMPUTE group = 8. + ELSE IF (Practitioner EQ 2). + COMPUTE group = 9. + END IF. END IF. EXECUTE. |
When I run the test data,
frequencies run with values 1-9, excluding 7.
Ed Tesiny From: SPSSX(r) Discussion
[mailto:[hidden email]] On Behalf Of Salbod, Mr. Stephen Dear Friends, I’m trying to combine following DO IF, IF, and DO IF
into one DO IF. I don’t want to recode sysmis into another value. When I’ve combined the codes under a single DO IF, and
run the test data, I get only groups 1-4 and the rest missing. Any help will be greatly appreciated. TIA Stephen Salbod, Pace University, NYC DATA LIST / Practitioner 1 StudentStat 2 . BEGIN DATA 11 12 21 22 1 2 1 2 END DATA. DO IF (Practitioner EQ 1 AND StudentStat EQ 1). COMPUTE group = 1. ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). COMPUTE group = 2. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). COMPUTE group = 3. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). COMPUTE group = 4. END IF. IF (MISSING(Practitioner) AND MISSING(StudentStat)) group =
5. DO IF (MISSING(Practitioner)). + DO IF (StudentStat EQ 1). +
COMPUTE group = 6. + ELSE IF
(StudentStat EQ 2). +
COMPUTE group = 7. + END IF. ELSE IF (MISSING(StudentStat)). + DO IF (Practitioner EQ 1). +
COMPUTE group = 8. + ELSE IF
(Practitioner EQ 2). +
COMPUTE group = 9. + END IF. END IF. EXECUTE. |
Administrator
|
In reply to this post by Salbod
All those nested DO-IFs get complicated. I'd do something like this instead.
recode practitioner StudentStat (missing=9). compute both_in_one = (practitioner*10) + StudentStat. format both_in_one (f2.0). freq both_in_one. * If you need the originally stated group numbers, * recode both_in_one into group. recode both_in_one (11 = 1) (12 = 2) (21 = 3) (22 = 4) (99 = 5) (91 = 6) (92 = 7) (19 = 8) (29 = 9) into group. freq group. * Check that recode worked as intended. crosstabs both_in_one by group. * Now assign value labels.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by Tesiny, Ed
Ed: I could have introduce a space error when I pasted from SPSS. The data should presented unique conditions. --Steve From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Tesiny, Ed When I run the test data, frequencies run with values 1-9, excluding 7.
Ed Tesiny From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Salbod, Mr. Stephen Dear Friends, I’m trying to combine following DO IF, IF, and DO IF into one DO IF. I don’t want to recode sysmis into another value. When I’ve combined the codes under a single DO IF, and run the test data, I get only groups 1-4 and the rest missing. Any help will be greatly appreciated. TIA Stephen Salbod, Pace University, NYC DATA LIST / Practitioner 1 StudentStat 2 . BEGIN DATA 11 12 21 22 1 2 1 2 END DATA. DO IF (Practitioner EQ 1 AND StudentStat EQ 1). COMPUTE group = 1. ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). COMPUTE group = 2. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). COMPUTE group = 3. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). COMPUTE group = 4. END IF. IF (MISSING(Practitioner) AND MISSING(StudentStat)) group = 5. DO IF (MISSING(Practitioner)). + DO IF (StudentStat EQ 1). + COMPUTE group = 6. + ELSE IF (StudentStat EQ 2). + COMPUTE group = 7. + END IF. ELSE IF (MISSING(StudentStat)). + DO IF (Practitioner EQ 1). + COMPUTE group = 8. + ELSE IF (Practitioner EQ 2). + COMPUTE group = 9. + END IF. END IF. EXECUTE. |
In reply to this post by Bruce Weaver
Hi Bruce: Thank you for an interesting solution. But, I was looking more for how to rewrite my code into a single DO IF structure. --Steve
-----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver Sent: Wednesday, November 17, 2010 10:38 AM To: [hidden email] Subject: Re: DO IF All those nested DO-IFs get complicated. I'd do something like this instead. recode practitioner StudentStat (missing=9). compute both_in_one = (practitioner*10) + StudentStat. format both_in_one (f2.0). freq both_in_one. * If you need the originally stated group numbers, * recode both_in_one into group. recode both_in_one (11 = 1) (12 = 2) (21 = 3) (22 = 4) (99 = 5) (91 = 6) (92 = 7) (19 = 8) (29 = 9) into group. freq group. * Check that recode worked as intended. crosstabs both_in_one by group. * Now assign value labels. Salbod, Mr. Stephen wrote: > > Dear Friends, > > I'm trying to combine following DO IF, IF, and DO IF into one DO IF. I > don't want to recode sysmis into another value. > When I've combined the codes under a single DO IF, and run the test > data, I get only groups 1-4 and the rest missing. > > Any help will be greatly appreciated. > > TIA > > Stephen Salbod, Pace University, NYC > > > DATA LIST / Practitioner 1 StudentStat 2 . > BEGIN DATA > 11 > 12 > 21 > 22 > > 1 > 2 > 1 > 2 > END DATA. > DO IF (Practitioner EQ 1 AND StudentStat EQ 1). > COMPUTE group = 1. > ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). > COMPUTE group = 2. > ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). > COMPUTE group = 3. > ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). > COMPUTE group = 4. > END IF. > IF (MISSING(Practitioner) AND MISSING(StudentStat)) group = 5. > DO IF (MISSING(Practitioner)). > + DO IF (StudentStat EQ 1). > + COMPUTE group = 6. > + ELSE IF (StudentStat EQ 2). > + COMPUTE group = 7. > + END IF. > ELSE IF (MISSING(StudentStat)). > + DO IF (Practitioner EQ 1). > + COMPUTE group = 8. > + ELSE IF (Practitioner EQ 2). > + COMPUTE group = 9. > + END IF. > END IF. > EXECUTE. > > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/DO-IF-tp3269165p3269218.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 |
Administrator
|
In reply to this post by Bruce Weaver
Right after posting, I noticed that Stephen does not want to recode SYSMIS to another value (for some reason). So I've modified the RECODE and computation of BOTH_IN_ONE as follows to avoid recoding SYSMIS to 9 in the original variables.
recode practitioner StudentStat (missing=9) (else=copy) into #P #S . compute both_in_one = (#P*10) + #S. format both_in_one (f2.0). freq both_in_one.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by Bruce Weaver
Excuse my obtuseness, Bruce, I see how your solution is parsimonious and
pretty. But out of curiosity why doesn't the combined do if work. Is there some logic of DO IF that I'm missing?: DO IF (Practitioner EQ 1 AND StudentStat EQ 1). COMPUTE group = 1. ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). COMPUTE group = 2. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). COMPUTE group = 3. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). COMPUTE group = 4. else if missing(Practitioner) AND missing(StudentStat). compute group = 5. else if (MISSING(Practitioner)). DO IF (StudentStat EQ 1). COMPUTE group = 6. ELSE IF (StudentStat EQ 2). COMPUTE group = 7. END IF. ELSE IF (MISSING(StudentStat)). DO IF (Practitioner EQ 1). COMPUTE group = 8. ELSE IF (Practitioner EQ 2). COMPUTE group = 9. END IF. END IF. EXECUTE. Or even if you just add the first else if with the missing criteria it doesn't work. What am I missing? DO IF (Practitioner EQ 1 AND StudentStat EQ 1). COMPUTE group = 1. ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). COMPUTE group = 2. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). COMPUTE group = 3. ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). COMPUTE group = 4. else IF (MISSING(Practitioner) AND MISSING(StudentStat)). compute group = 5. end if. exe. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver Sent: Wednesday, November 17, 2010 7:38 AM To: [hidden email] Subject: Re: DO IF All those nested DO-IFs get complicated. I'd do something like this instead. recode practitioner StudentStat (missing=9). compute both_in_one = (practitioner*10) + StudentStat. format both_in_one (f2.0). freq both_in_one. * If you need the originally stated group numbers, * recode both_in_one into group. recode both_in_one (11 = 1) (12 = 2) (21 = 3) (22 = 4) (99 = 5) (91 = 6) (92 = 7) (19 = 8) (29 = 9) into group. freq group. * Check that recode worked as intended. crosstabs both_in_one by group. * Now assign value labels. Salbod, Mr. Stephen wrote: > > Dear Friends, > > I'm trying to combine following DO IF, IF, and DO IF into one DO IF. I > don't want to recode sysmis into another value. > When I've combined the codes under a single DO IF, and run the test data, > I get only groups 1-4 and the rest missing. > > Any help will be greatly appreciated. > > TIA > > Stephen Salbod, Pace University, NYC > > > DATA LIST / Practitioner 1 StudentStat 2 . > BEGIN DATA > 11 > 12 > 21 > 22 > > 1 > 2 > 1 > 2 > END DATA. > DO IF (Practitioner EQ 1 AND StudentStat EQ 1). > COMPUTE group = 1. > ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). > COMPUTE group = 2. > ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). > COMPUTE group = 3. > ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). > COMPUTE group = 4. > END IF. > IF (MISSING(Practitioner) AND MISSING(StudentStat)) group = 5. > DO IF (MISSING(Practitioner)). > + DO IF (StudentStat EQ 1). > + COMPUTE group = 6. > + ELSE IF (StudentStat EQ 2). > + COMPUTE group = 7. > + END IF. > ELSE IF (MISSING(StudentStat)). > + DO IF (Practitioner EQ 1). > + COMPUTE group = 8. > + ELSE IF (Practitioner EQ 2). > + COMPUTE group = 9. > + END IF. > END IF. > EXECUTE. > > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/DO-IF-tp3269165p3269218.ht ml 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 |
In reply to this post by Bruce Weaver
Bruce: To deal with the actual problem I did use recode. Now, I'm trying to understand why I cannot combine DO IF, IF, and DO IF into a single statement. I wondering if I'm missing a logical step related to short circuit behavior with sysmis. --Steve
-----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver Sent: Wednesday, November 17, 2010 10:57 AM To: [hidden email] Subject: Re: DO IF Right after posting, I noticed that Stephen does not want to recode SYSMIS to another value (for some reason). So I've modified the RECODE and computation of BOTH_IN_ONE as follows to avoid recoding SYSMIS to 9 in the original variables. recode practitioner StudentStat (missing=9) (else=copy) into #P #S . compute both_in_one = (#P*10) + #S. format both_in_one (f2.0). freq both_in_one. Bruce Weaver wrote: > > All those nested DO-IFs get complicated. I'd do something like this > instead. > > recode practitioner StudentStat (missing=9). > > compute both_in_one = (practitioner*10) + StudentStat. > format both_in_one (f2.0). > freq both_in_one. > > * If you need the originally stated group numbers, > * recode both_in_one into group. > > recode both_in_one > (11 = 1) > (12 = 2) > (21 = 3) > (22 = 4) > (99 = 5) > (91 = 6) > (92 = 7) > (19 = 8) > (29 = 9) into group. > freq group. > * Check that recode worked as intended. > crosstabs both_in_one by group. > > * Now assign value labels. > > > > Salbod, Mr. Stephen wrote: >> >> Dear Friends, >> >> I'm trying to combine following DO IF, IF, and DO IF into one DO IF. >> I don't want to recode sysmis into another value. >> When I've combined the codes under a single DO IF, and run the test >> data, I get only groups 1-4 and the rest missing. >> >> Any help will be greatly appreciated. >> >> TIA >> >> Stephen Salbod, Pace University, NYC >> >> >> DATA LIST / Practitioner 1 StudentStat 2 . >> BEGIN DATA >> 11 >> 12 >> 21 >> 22 >> >> 1 >> 2 >> 1 >> 2 >> END DATA. >> DO IF (Practitioner EQ 1 AND StudentStat EQ 1). >> COMPUTE group = 1. >> ELSE IF (Practitioner EQ 1 AND StudentStat EQ 2). >> COMPUTE group = 2. >> ELSE IF (Practitioner EQ 2 AND StudentStat EQ 1). >> COMPUTE group = 3. >> ELSE IF (Practitioner EQ 2 AND StudentStat EQ 2). >> COMPUTE group = 4. >> END IF. >> IF (MISSING(Practitioner) AND MISSING(StudentStat)) group = 5. >> DO IF (MISSING(Practitioner)). >> + DO IF (StudentStat EQ 1). >> + COMPUTE group = 6. >> + ELSE IF (StudentStat EQ 2). >> + COMPUTE group = 7. >> + END IF. >> ELSE IF (MISSING(StudentStat)). >> + DO IF (Practitioner EQ 1). >> + COMPUTE group = 8. >> + ELSE IF (Practitioner EQ 2). >> + COMPUTE group = 9. >> + END IF. >> END IF. >> EXECUTE. >> >> > > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/DO-IF-tp3269165p3269261.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 |
Administrator
|
In reply to this post by Salbod
I can't imagine why you would prefer a nasty nested DO-IF, but it would look like this, I think. * To avoid RSI, rename the variables first. rename variables (practitioner StudentStat = p s). * To avoid using the Shift key, use - rather than + * at start of indented lines do if (p EQ 1). - do if (s EQ 1) - compute group = 1. - else if (s EQ 2). - compute group = 2. - else if missing(s). - compute group = 8. - end if. else if (p EQ 2). - do if (s EQ 1) - compute group = 3. - else if (s EQ 2). - compute group = 4. - else if missing(s). - compute group = 9. - end if. else if missing(p). - do if (s EQ 1) - compute group = 6. - else if (s EQ 2). - compute group = 7. - else if missing(s). - compute group = 5. - end if. end if. exe. * Revert to original variable names. rename variables (p s = pracitioner StudentStat).
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
Sorry, I should have tested that before posting. It had missing command terminators on some lines, and also had a typo in the last RENAME VARIABLES command. But even after I fixed those problems, it only generated group numbers 1-4. I cannot immediately see why it generated SYSMIS for the other group numbers. (This is one reason why I like my *other* solution a lot more than a nasty nested DO-IF.) Nevertheless, here is a nested DO-IF that does generate the same group numbers as my other solution. * To avoid RSI, recode the two variables into * scratch variables with short names. * And recode SYSMIS into 9 so that missing * values don't louse things up. recode Practitioner StudentStat (missing=9) (else=copy) into #P #S. * To avoid using the Shift key, use - rather than + * at start of indented lines do if (#P EQ 1). - do if (#S EQ 1). - compute group = 1. - else if (#S EQ 2). - compute group = 2. - else if (#S EQ 9). - compute group = 8. - end if. else if (#P EQ 2). - do if (#S EQ 1). - compute group = 3. - else if (#S EQ 2). - compute group = 4. - else if (#S EQ 9). - compute group = 9. - end if. else if (#P EQ 9). - do if (#S EQ 1). - compute group = 6. - else if (#S EQ 2). - compute group = 7. - else if (#S EQ 9). - compute group = 5. - end if. end if. exe.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by Bruce Weaver
At 11:22 AM 11/17/2010, Bruce Weaver wrote:
>A nested DO-IF would look like this, I think. The following won't work: >do if (p EQ 1). >- do if (s EQ 1) >- compute group = 1. >- else if (s EQ 2). >- compute group = 2. >- else if missing(s). >- compute group = 8. >- end if. >else if (p EQ 2). >- do if (s EQ 1) >- compute group = 3. >- else if (s EQ 2). >- compute group = 4. >- else if missing(s). >- compute group = 9. >- end if. >else if missing(p). >- do if (s EQ 1) >- compute group = 6. >- else if (s EQ 2). >- compute group = 7. >- else if missing(s). >- compute group = 5. >- end if. >end if. The outer and inner "DO IF" constructs end with "else if missing" tests. Those will never be executed; "group" will not be computed in any case where either "p" or "s" are missing. (A pitfall of "DO IF" is that if any test returns 'missing', NONE OF THE LATER TESTS ARE CARRIED OUT, and there is NO WARNING. The original poster's code may have failed for the same reason.) The trick is to put the 'missing' test FIRST on the list, like this (not tested): DO IF missing(p). . DO IF missing(s). . compute group = 5. . ELSE IF (s EQ 1). . compute group = 6. . else if (s EQ 2). . compute group = 7. . end if. ELSE IF (p EQ 1). . DO IF missing(s). . compute group = 8. . ELSE IF (s EQ 1). . compute group = 1. . else if (s EQ 2). . compute group = 2. . end if. else if (p EQ 2). . DO IF missing(s). . compute group = 9. . ELSE IF (s EQ 1). . compute group = 3. . else if (s EQ 2). . compute group = 4. . end if. end if. ===================== 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
|
Thanks for the clarification, Richard. I knew that when a TRUE condition is encountered, the specified operations for that condition are carried out, and then the DO-IF structure is exited. I'm not sure if I ever had a good grasp on what happens when a condition returns MISSING.
For sake of completeness, here is what the CSR Manual says about flow of control in a DO-IF structure. Flow of Control - If the logical expression on DO IF is true, the commands immediately following DO IF are executed up to the next ELSE IF, ELSE, or END IF command. Control then passes to the first statement following END IF. - If the expression on DO IF is false, control passes to the following ELSE IF command. Multiple ELSE IF commands are evaluated in the order in which they are specified until the logical expression on one of them is true. Commands following that ELSE IF command are executed up to the ELSE or END IF command, and control passes to the first statement following END IF. - If none of the expressions are true on the DO IF or any of the ELSE IF commands, the commands following ELSE are executed and control passes out of the structure. If there is no ELSE command, a case goes through the entire structure with no change. - Missing values returned by the logical expression on DO IF or on any ELSE IF cause control to pass to the END IF command at that point.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Much a DO about nothing <ducking and running>
________________________________ From: SPSSX(r) Discussion on behalf of Bruce Weaver Sent: Wed 11/17/2010 4:52 PM To: [hidden email] Subject: Re: DO IF Thanks for the clarification, Richard. I knew that when a TRUE condition is encountered, the specified operations for that condition are carried out, and then the DO-IF structure is exited. I'm not sure if I ever had a good grasp on what happens when a condition returns MISSING. For sake of completeness, here is what the CSR Manual says about flow of control in a DO-IF structure. Flow of Control - If the logical expression on DO IF is true, the commands immediately following DO IF are executed up to the next ELSE IF, ELSE, or END IF command. Control then passes to the first statement following END IF. - If the expression on DO IF is false, control passes to the following ELSE IF command. Multiple ELSE IF commands are evaluated in the order in which they are specified until the logical expression on one of them is true. Commands following that ELSE IF command are executed up to the ELSE or END IF command, and control passes to the first statement following END IF. - If none of the expressions are true on the DO IF or any of the ELSE IF commands, the commands following ELSE are executed and control passes out of the structure. If there is no ELSE command, a case goes through the entire structure with no change. - Missing values returned by the logical expression on DO IF or on any ELSE IF cause control to pass to the END IF command at that point. Richard Ristow wrote: > > At 11:22 AM 11/17/2010, Bruce Weaver wrote: > >>A nested DO-IF would look like this, I think. > > The following won't work: > >>do if (p EQ 1). >>- do if (s EQ 1) >>- compute group = 1. >>- else if (s EQ 2). >>- compute group = 2. >>- else if missing(s). >>- compute group = 8. >>- end if. >>else if (p EQ 2). >>- do if (s EQ 1) >>- compute group = 3. >>- else if (s EQ 2). >>- compute group = 4. >>- else if missing(s). >>- compute group = 9. >>- end if. >>else if missing(p). >>- do if (s EQ 1) >>- compute group = 6. >>- else if (s EQ 2). >>- compute group = 7. >>- else if missing(s). >>- compute group = 5. >>- end if. >>end if. > > The outer and inner "DO IF" constructs end with "else if missing" > tests. Those will never be executed; "group" will not be computed in > any case where either "p" or "s" are missing. > > (A pitfall of "DO IF" is that if any test returns 'missing', NONE OF > THE LATER TESTS ARE CARRIED OUT, and there is NO WARNING. The > original poster's code may have failed for the same reason.) > > The trick is to put the 'missing' test FIRST on the list, like this > (not tested): > > DO IF missing(p). > . DO IF missing(s). > . compute group = 5. > . ELSE IF (s EQ 1). > . compute group = 6. > . else if (s EQ 2). > . compute group = 7. > . end if. > ELSE IF (p EQ 1). > . DO IF missing(s). > . compute group = 8. > . ELSE IF (s EQ 1). > . compute group = 1. > . else if (s EQ 2). > . compute group = 2. > . end if. > else if (p EQ 2). > . DO IF missing(s). > . compute group = 9. > . ELSE IF (s EQ 1). > . compute group = 3. > . else if (s EQ 2). > . compute group = 4. > . end if. > end if. > > ===================== > 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 > > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/DO-IF-tp3269165p3269865.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 |
Administrator
|
In reply to this post by Salbod
data list free / Practitioner StudentStat.
begin data 1 1 1 2 2 1 2 2 . . . 1 . 2 1 . 2 . end data. IF NOT (MISSING(Practitioner ) OR MISSING(StudentStat)) Group = (Practitioner -1)*2+StudentStat. IF MISSING(Practitioner ) Group = 5+StudentStat. IF MISSING(StudentStat) Group = 7+Practitioner . If Missing(Practitioner ) and Missing(StudentStat) Group = 5. LIST.
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 Bruce Weaver
Bruce: There are no missing values being evaluated in the DO IF structure if MISSING returns a value ( see line #2 of code).
I painted (one line at a time) a solution this morning. I still don't understand why the inverse works. --Steve DO IF missing(p) and missing(s). COMPUTE newGroup = missing(p) + missing(s) + 3. ELSE IF missing(p) AND s = 1. COMPUTE newGroup = 6. ELSE IF missing(p) AND s = 2. COMPUTE newGroup = 7. ELSE IF p = 1 AND missing(s). COMPUTE newGroup = 8. ELSE IF p = 2 AND missing(s). COMPUTE newGroup = 9. ELSE IF p = 1 AND s = 1. COMPUTE newGroup = 1. ELSE IF p = 1 AND s = 2. COMPUTE newGroup = 2. ELSE IF p = 2 AND s = 1. COMPUTE newGroup = 3. ELSE IF p = 2 AND s = 2. COMPUTE newGroup = 4. END IF. EXECUTE. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver Sent: Wednesday, November 17, 2010 4:52 PM To: [hidden email] Subject: Re: DO IF Thanks for the clarification, Richard. I knew that when a TRUE condition is encountered, the specified operations for that condition are carried out, and then the DO-IF structure is exited. I'm not sure if I ever had a good grasp on what happens when a condition returns MISSING. For sake of completeness, here is what the CSR Manual says about flow of control in a DO-IF structure. Flow of Control - If the logical expression on DO IF is true, the commands immediately following DO IF are executed up to the next ELSE IF, ELSE, or END IF command. Control then passes to the first statement following END IF. - If the expression on DO IF is false, control passes to the following ELSE IF command. Multiple ELSE IF commands are evaluated in the order in which they are specified until the logical expression on one of them is true. Commands following that ELSE IF command are executed up to the ELSE or END IF command, and control passes to the first statement following END IF. - If none of the expressions are true on the DO IF or any of the ELSE IF commands, the commands following ELSE are executed and control passes out of the structure. If there is no ELSE command, a case goes through the entire structure with no change. - Missing values returned by the logical expression on DO IF or on any ELSE IF cause control to pass to the END IF command at that point. Richard Ristow wrote: > > At 11:22 AM 11/17/2010, Bruce Weaver wrote: > >>A nested DO-IF would look like this, I think. > > The following won't work: > >>do if (p EQ 1). >>- do if (s EQ 1) >>- compute group = 1. >>- else if (s EQ 2). >>- compute group = 2. >>- else if missing(s). >>- compute group = 8. >>- end if. >>else if (p EQ 2). >>- do if (s EQ 1) >>- compute group = 3. >>- else if (s EQ 2). >>- compute group = 4. >>- else if missing(s). >>- compute group = 9. >>- end if. >>else if missing(p). >>- do if (s EQ 1) >>- compute group = 6. >>- else if (s EQ 2). >>- compute group = 7. >>- else if missing(s). >>- compute group = 5. >>- end if. >>end if. > > The outer and inner "DO IF" constructs end with "else if missing" > tests. Those will never be executed; "group" will not be computed in > any case where either "p" or "s" are missing. > > (A pitfall of "DO IF" is that if any test returns 'missing', NONE OF > THE LATER TESTS ARE CARRIED OUT, and there is NO WARNING. The original > poster's code may have failed for the same reason.) > > The trick is to put the 'missing' test FIRST on the list, like this > (not tested): > > DO IF missing(p). > . DO IF missing(s). > . compute group = 5. > . ELSE IF (s EQ 1). > . compute group = 6. > . else if (s EQ 2). > . compute group = 7. > . end if. > ELSE IF (p EQ 1). > . DO IF missing(s). > . compute group = 8. > . ELSE IF (s EQ 1). > . compute group = 1. > . else if (s EQ 2). > . compute group = 2. > . end if. > else if (p EQ 2). > . DO IF missing(s). > . compute group = 9. > . ELSE IF (s EQ 1). > . compute group = 3. > . else if (s EQ 2). > . compute group = 4. > . end if. > end if. > > ===================== > 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 > > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/DO-IF-tp3269165p3269865.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 |
Administrator
|
Hi Stephen. Here's the version I posted--i.e., a version that assigned values of GROUP only for groups 1-4.
DO IF (p EQ 1). /* Line A. . DO IF (s EQ 1). /* Line B. . compute group = 1. . else if (s EQ 2). . compute group = 2. . else IF missing(s). . compute group = 8. . end if. else if (p EQ 2). . DO IF (s EQ 1). /* Line C. . compute group = 3. . else if (s EQ 2). . compute group = 4. . else IF missing(s). . compute group = 9. . end if. ELSE IF missing(p). . DO IF (s EQ 1). . compute group = 6. . else if (s EQ 2). . compute group = 7. . else IF missing(s). . compute group = 5. . end if. end if. Think about what will happen if P is missing. The condition (p EQ 1) on Line A above will return a value of missing, because P is missing. According to the Flow of Control info from the CSR Manual (see below), "Missing values returned by the logical expression on DO IF or on any ELSE IF cause control to pass to the END IF command at that point". So the DO-IF structure would be exited, and no value assigned to GROUP. If P=1 or P=2, but S is missing, the condition (s EQ 1) on either Line B or C above will return a value of missing, and again, the DO-IF structure will be exited with nothing being assigned to Group. In Richard's version (below), on the other hand, the tests for MISSING(P) and MISSING(S) come first. Therefore, all of the conditions will return a value of either 1 (true) or 0 (false), and all group numbers will be assigned correctly. DO IF missing(p). . DO IF missing(s). . compute group = 5. . ELSE IF (s EQ 1). . compute group = 6. . else if (s EQ 2). . compute group = 7. . end if. ELSE IF (p EQ 1). . DO IF missing(s). . compute group = 8. . ELSE IF (s EQ 1). . compute group = 1. . else if (s EQ 2). . compute group = 2. . end if. else if (p EQ 2). . DO IF missing(s). . compute group = 9. . ELSE IF (s EQ 1). . compute group = 3. . else if (s EQ 2). . compute group = 4. . end if. end if. HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Thank you for clearing up my brain fog. Boy, was I stuck in the box.
Expressions containing Missing(p) (and/or Missing(s)) have to come first or else the expression p EQ 1 (or 2) (and/or s EQ 1 (or 2)) evaluates to missing and the compute statement is not executed. --Steve -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bruce Weaver Sent: Thursday, November 18, 2010 10:30 AM To: [hidden email] Subject: Re: DO IF Hi Stephen. Here's the version I posted--i.e., a version that assigned values of GROUP only for groups 1-4. DO IF (p EQ 1). /* Line A. . DO IF (s EQ 1). /* Line B. . compute group = 1. . else if (s EQ 2). . compute group = 2. . else IF missing(s). . compute group = 8. . end if. else if (p EQ 2). . DO IF (s EQ 1). /* Line C. . compute group = 3. . else if (s EQ 2). . compute group = 4. . else IF missing(s). . compute group = 9. . end if. ELSE IF missing(p). . DO IF (s EQ 1). . compute group = 6. . else if (s EQ 2). . compute group = 7. . else IF missing(s). . compute group = 5. . end if. end if. Think about what will happen if P is missing. The condition (p EQ 1) on Line A above will return a value of missing, because P is missing. According to the Flow of Control info from the CSR Manual (see below), "Missing values returned by the logical expression on DO IF or on any ELSE IF cause control to pass to the END IF command at that point". So the DO-IF structure would be exited, and no value assigned to GROUP. If P=1 or P=2, but S is missing, the condition (s EQ 1) on either Line B or C above will return a value of missing, and again, the DO-IF structure will be exited with nothing being assigned to Group. In Richard's version (below), on the other hand, the tests for MISSING(P) and MISSING(S) come first. Therefore, all of the conditions will return a value of either 1 (true) or 0 (false), and all group numbers will be assigned correctly. DO IF missing(p). . DO IF missing(s). . compute group = 5. . ELSE IF (s EQ 1). . compute group = 6. . else if (s EQ 2). . compute group = 7. . end if. ELSE IF (p EQ 1). . DO IF missing(s). . compute group = 8. . ELSE IF (s EQ 1). . compute group = 1. . else if (s EQ 2). . compute group = 2. . end if. else if (p EQ 2). . DO IF missing(s). . compute group = 9. . ELSE IF (s EQ 1). . compute group = 3. . else if (s EQ 2). . compute group = 4. . end if. end if. HTH. Salbod, Mr. Stephen wrote: > > Bruce: There are no missing values being evaluated in the DO IF > structure if MISSING returns a value ( see line #2 of code). > > I painted (one line at a time) a solution this morning. I still don't > understand why the inverse works. --Steve > > DO IF missing(p) and missing(s). > COMPUTE newGroup = missing(p) + missing(s) + 3. > ELSE IF missing(p) AND s = 1. > COMPUTE newGroup = 6. > ELSE IF missing(p) AND s = 2. > COMPUTE newGroup = 7. > ELSE IF p = 1 AND missing(s). > COMPUTE newGroup = 8. > ELSE IF p = 2 AND missing(s). > COMPUTE newGroup = 9. > ELSE IF p = 1 AND s = 1. > COMPUTE newGroup = 1. > ELSE IF p = 1 AND s = 2. > COMPUTE newGroup = 2. > ELSE IF p = 2 AND s = 1. > COMPUTE newGroup = 3. > ELSE IF p = 2 AND s = 2. > COMPUTE newGroup = 4. > END IF. > EXECUTE. > > -----Original Message----- > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf > Of Bruce Weaver > Sent: Wednesday, November 17, 2010 4:52 PM > To: [hidden email] > Subject: Re: DO IF > > For sake of completeness, here is what the CSR Manual says about flow > of control in a DO-IF structure. > > > Flow of Control > > - If the logical expression on DO IF is true, the commands immediately > following DO IF are executed up to the next ELSE IF, ELSE, or END IF > command. Control then passes to the first statement following END IF. > > - If the expression on DO IF is false, control passes to the following > ELSE IF command. Multiple ELSE IF commands are evaluated in the order > in which they are specified until the logical expression on one of > them is true. > Commands following that ELSE IF command are executed up to the ELSE or > END IF command, and control passes to the first statement following END IF. > > - If none of the expressions are true on the DO IF or any of the ELSE > IF commands, the commands following ELSE are executed and control > passes out of the structure. If there is no ELSE command, a case goes > through the entire structure with no change. > > - Missing values returned by the logical expression on DO IF or on any > ELSE IF cause control to pass to the END IF command at that point. > > ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/DO-IF-tp3269165p3270900.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 |
Administrator
|
Right. And in case anyone else is still struggling, the code below demonstrates what Steve means when he says "the expression p EQ 1 (or 2) (and/or s EQ 1 (or 2)) evaluates to missing". DATA LIST / P 1 S 2 . BEGIN DATA 11 12 21 22 1 2 1 2 END DATA. compute pEQ1 = (p EQ 1). compute pEQ2 = (p EQ 2). compute sEQ1 = (s EQ 1). compute sEQ2 = (s EQ 2). format peq1 to seq1 (f1.0). list. OUTPUT: P S pEQ1 pEQ2 sEQ1 sEQ2 1 1 1 0 1 0 1 2 1 0 0 1 2 1 0 1 1 0 2 2 0 1 0 1 . . . . . . 1 . 1 0 . . 2 . 0 1 . . . 1 . . 1 0 . 2 . . 0 1 Number of cases read: 9 Number of cases listed: 9
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Free forum by Nabble | Edit this page |