```Date: Sun, 23 Jan 2005 18:03:15 -0800 Reply-To: Howie Harshaw Sender: "SPSSX(r) Discussion" From: Howie Harshaw Subject: Re: DO IF ELSE syntax question Comments: To: Richard Ristow In-Reply-To: <5.1.0.14.2.20050123185204.067a9920@pop.mindspring.com> Content-type: text/plain; charset=ISO-8859-1; format=flowed Thank you Richard for cleaning up and correcting my syntax - with a slight modification (below in the 3rd ELSE IF statement) it runs perfectly. Thanks too for the walk through SPSS logic - I have a better understanding of why what I was doing wasn't working, and will follow up with the syntax manual as you suggest. - Howie DO IF (NMISS(recimp1r,recimp2r,recimp3r) = 3). . Compute rec_spec1 = 0. ELSE IF (NMISS(recimp1r,recimp2r,recimp3r) = 0). . Compute rec_spec1 = 1. ELSE IF ((NOT MISSING(recimp1r)) AND (NOT MISSING(recimp2r)) AND (MISSING(recimp3r))). . Compute rec_spec1 = 2. ELSE IF ((NOT MISSING(recimp1r)) AND (MISSING(recimp2r)) AND (MISSING(recimp3r))). . Compute rec_spec1 = 3. END IF. EXECUTE. _______________________________ Howard Harshaw Ph.D. Candidate Forest Resource Management Faculty of Forestry University of British Columbia Tel: (604) 786-3141 E-mail: harshaw@interchange.ubc.ca Web: www.Harfolk.ca Richard Ristow wrote: > This is the beginning of an answer. What you're trying to do is > reasonable, but there are several problems with how you're trying to do it. > > At 05:07 PM 1/23/2005, Howie Harshaw wrote: > >> I have four numeric variables: >> recimp1r: values = 1-45; 999 = user missing >> recimp2r: values = 1-45; 999 = user missing >> recimp3r: values = 1-45; 999 = user missing >> rec_spec1 (empty) >> >> I would like to assign, >> "0" to rec_spec1 if recimp1r, recimp2r, and recimp3r all contain >> missing values (i.e. 999); >> "1" to rec_spec1 if recimp1r, recimp2r, and recimp3r do not contain >> missing values (i.e. 999) [QUESTION: That is, if none of them do?]; >> "2" to rec_spec1 if recimp1r and recimp2r do not contain missing >> values (i.e. 999) but recimp3r does; >> "3" to rec_spec1 if recimp1r does not contain missing values (i.e. >> 999) but and recimp2r recimp3r do. > > > Let me try it in pieces. Before I start, you'll see below that I've > written lines like this: > . Compute rec_spec1 = 0. > or like this: > - DO IF (NMISS(recimp1r,recimp2r,recimp3r)=3). > Those are indents. If you start an SPSS command line with "." or "-", > you can put in as many spaces as you like before the keyword. Those > statements are the same as > Compute rec_spec1 = 0. > DO IF (NMISS(recimp1r,recimp2r,recimp3r)=3). > > But now, to your problem. You begin with, > >> DO IF ((recimp1r = 999 AND recimp2r = 999) AND recimp3r = 999). >> . Compute rec_spec1 = 0. > > > This isn't causing the problem you're seeing, but it won't work, either. > With your data, the tests "recimp1r=999", etc., will *NEVER* be true. > Remember, 999 is a missing value. If recimpr1r is 999, SPSS will > interpret it as "value of recimpr1r unknown", and the result is > "missing". ("Missing" is neither 'true' nor 'false'.) > > The following variations will work: > GOOD (if the variables are never system-missing): > - DO IF ((VALUE(recimp1r) = 999 AND VALUE(recimp2r) = 999) > AND VALUE(recimp3r) = 999). > . Compute rec_spec1 = 0. > BETTER (will detect both '999' and system-missing): > - DO IF ((MISSING(recimp1r) AND MISSING(recimp2r) > AND MISSING(recimp3r)). > . Compute rec_spec1 = 0. > BEST (will detect both '999' and system-missing): > - DO IF (NMISS(recimp1r,recimp2r,recimp3r)=3). > . Compute rec_spec1 = 0. > >> ELSE. >> IF ((recimp1r <> 999 AND recimp2r <> 999) AND recimp3r <> 999). >> . Compute rec_spec1 = 1. > > > This may be the problem you're seeing. If you mean "ELSE IF", that is a > *single* statement. You have two statements: > ELSE. > IF . > > This has a meaning, but probably not the one you want. So, > - ELSE IF ((recimp1r <> 999 AND recimp2r <> 999) AND recimp3r <> 999). > . Compute rec_spec1 = 1. > > But that won't work if any of the values is 999. A good alternative is > - ELSE IF (NMISS(recimp1r,recimp2r,recimp3r) = 0). > . Compute rec_spec1 = 1. > > But, to go back to where you were. You have, > >> ELSE. >> IF ((recimp1r <> 999 AND recimp2r <> 999) AND recimp3r <> 999). >> . Compute rec_spec1 = 1. > > > There is a statement named "IF". It performs a test and an assignment > (like a "COMPUTE") together. Syntax (from the syntax manual) is, > IF [(]logical expression[)] target variable=expression > (The square brackets mean you can put the parentheses in, or leave them > out.) > > I think that > . IF ((recimp1r <> 999 AND recimp2r <> 999) AND recimp3r <> 999). > is giving you the error message. > >> >Error # 4382 in column 256. Text: (End of Command) >> >An equals sign was not found when expected after a target variable in a >> >COMPUTE command. >> >This command not executed. > > > You have an "IF" statement with the test, but without > "variable=expression", and that's confusing SPSS. > > In the syntax manual, you should read, carefully, > . "Missing Values in Logical Expressions", in section "Transformation > Expressions", near the beginning (but, I'm afraid, it's not as clear as > one would like) > . The section on IF > . The section on DO IF. In particular, try read the following until it > makes sense. (And do not be afraid to post further questions.): > "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 or 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. > "· [THIS IS THE TRICKY ONE] 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[, that is, to skip the rest of the DO IF > structure]." > > In the meantime, this should work (but I have not tested it): > > DO IF (NMISS(recimp1r,recimp2r,recimp3r) = 3). > . Compute rec_spec1 = 0. > ELSE IF (NMISS(recimp1r,recimp2r,recimp3r) = 0. > . Compute rec_spec1 = 1. > ELSE IF ((NOT MISSING(recimp1r)) AND (NOT MISSING(recimp2r)) > AND (MISSING(recimp3r))). > . Compute rec_spec1 = 2. > ELSE IF ( (MISSING(recimp1r)) AND (MISSING(recimp2r)) > AND (NOT MISSING(recimp3r))999). > . Compute rec_spec1 = 3. > END IF. > > ```

Back to: Top of message | Previous page | Main SPSSX-L page