Re: SPSSX-L Digest - 23 Mar 2007 to 24 Mar 2007 (#2007-84) Multiple If Statements

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: SPSSX-L Digest - 23 Mar 2007 to 24 Mar 2007 (#2007-84) Multiple If Statements

Leslie Horst
I've never seen "any" used this way, but it looks like a slick solution.  However, since "to" is used (and reads variables sequentially in their order in the file), I would just make sure that there are no variables in the file between proccode1 and proccode104 that you do not want to include in the check for the various values.  If there are extraneous variables, maybe you could write the statements as:

 IF (ANY(1, proccode1, proccode2,  proccode104)) code1=1.
(etc.)

I have not tested this.

Leslie Horst
Maguire Associates, Inc.
Concord, MA  01742


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Automatic digest processor
Sent: Sunday, March 25, 2007 12:00 AM
To: Recipients of SPSSX-L digests
Subject: SPSSX-L Digest - 23 Mar 2007 to 24 Mar 2007 (#2007-84)
Date:    Sat, 24 Mar 2007 14:14:57 -0400
From:    Giuseppina Chiri <[hidden email]>
Subject: multiple IF statements

Dear list,
I have a series of variables (procode1 to procode104) coded 1 thru 9. I would like to compute new variables (code1 to code9) based on whether the original variable had a certain code in it or not. See example below. I have searched the list's archives but could not figure out a good way of doing this. Can anyone suggest a posting and/or syntax?
Any help would be greatly appreciated.
Thanks


IF (proccode1 =1 or proccode2 = 1 or … proccode104=1) code1=1.
IF (proccode1 =2 or proccode2 = 2 or … proccode104=2) code2=1.
IF (proccode1 =3 or proccode2 = 3 or … proccode104=3) code3=1.
IF (proccode1 =4 or proccode2 = 4 or … proccode104=4) code4=1.
IF (proccode1 =5 or proccode2 = 5 or … proccode104=5) code5=1.
IF (proccode1 =6 or proccode2 = 6 or … proccode104=6) code6=1.
IF (proccode1 =7 or proccode2 = 7 or … proccode104=7) code7=1.
IF (proccode1 =8 or proccode2 = 8 or … proccode104=8) code8=1.
IF (proccode1 =9 or proccode2 = 9 or … proccode104=9) code9=1.

------------------------------

Date:    Sat, 24 Mar 2007 14:31:17 -0400
From:    James Cantor <[hidden email]>
Subject: Re: multiple IF statements

I think you may be looking for the "ANY" function.

IF (ANY(1, proccode1 TO proccode104)) code1=1.
IF (ANY(2, proccode1 TO proccode104)) code2=1.
IF (ANY(3, proccode1 TO proccode104)) code3=1.
IF (ANY(4, proccode1 TO proccode104)) code4=1.
IF (ANY(5, proccode1 TO proccode104)) code5=1.
IF (ANY(6, proccode1 TO proccode104)) code6=1.
IF (ANY(7, proccode1 TO proccode104)) code7=1.
IF (ANY(8, proccode1 TO proccode104)) code8=1.
IF (ANY(9, proccode1 TO proccode104)) code9=1.

The first argument of the ANY function is the value you are looking for, and the remaining arguments are the variables in which you are looking for that value.

The ANY function can also be used like this:

IF (ANY(var1, 1, 2, 3, 5, 7, 11)) flag=1.

In this usage, the first argument is the single variable you are examining, and the remaining arguments are the values you are checking for.  In this example, the value of "flag" would be set to 1 if the variable var1 were equal to any of the values listed.

- James Cantor