Hey SPSS Listers,
Is there any command similar to ANY statement for multiple AND conditions in SPSS? I frequently use ANY statement when it requires selecting records on the basis of multiple OR condition. For example : Select If ANY(ID,1,2,3). I am curious to know any statement which allows to subset data on the basis of multiple AND condition. For example : If ID = 1 AND VAR1 = 2 AND VAR2 = 3 AND VAR3 = 4. Can the above command be shortened? |
Administrator
|
Riya,
Notice that IF ANY(ID,1,2,3) is shorthand for IF ID=1 OR ID=2 OR ID=3. See there is a single source of variation and a constant. Your requested AND has 2 sources of variation so how would that work syntactically? ID = 1 AND VAR1 = 2 AND VAR2 = 3 AND VAR3 = 4 Maybe something of the form. COMPUTE ALL4=SUM(ID=1,V1=2,V2=3,V3=4 ) EQ 4. data list free/ id v1 to v3 . begin data 1 2 3 4 2 1 2 3 3 4 3 2 4 2 3 4 end data. COMPUTE ALL4=SUM(ID=1,V1=2,V2=3,V3=4 ) EQ 4. LIST. id v1 v2 v3 ALL4 1.00 2.00 3.00 4.00 1.00 2.00 1.00 2.00 3.00 .00 3.00 4.00 3.00 2.00 .00 4.00 2.00 3.00 4.00 .00 Number of cases read: 4 Number of cases listed: 4
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?" |
This post was updated on .
Hi Riya,
Along the lines of logic that David aka Mr Tech God suggested : COMPUTE Condition = MEAN(ID=1,VAR1=2,VAR2=3,VAR3=4 )= 1. EXECUTE. The above mean statement will always return 1. |
Administrator
|
Beautiful Ujjawal!!!
Much nicer solution since we don't need to know the number of elements (What was I thinking -BF-: Bruce must be taking the day off ;-) )! -- BTW: Please don't refer to me as Tech God ;-( Being a student of history, myself and whatever deities that may or may not exist don't exactly constitute a mutual admiration society ;-) I know you mean well but ... --
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Thank you for your lovely words..This really means a lot to me..
I think *SPSS Guru* better describes you ;) |
Administrator
|
In reply to this post by David Marso
I did look at it earlier, and was also thinking about MEAN, as it happens. ;-) But I was thinking that in the event of possible missing data (and depending on what one wants to do about it), perhaps it should be:
COMPUTE Condition = MEAN.4(ID EQ 1,VAR1 EQ 2,VAR2 EQ 3,VAR3 EQ 4) EQ 1. Like David's first version, this requires one to know the number of variables. Notice too that I followed Art K's advice, which is to reserve = for assignment of value to variable, and to use EQ, NE, LE, GE, GT or LT when testing conditions.
--
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/). |
@ Bruce : Thanks for pointing out limitation of my suggested formula :)) It made me think of writing another formula that doesn't requires one to know the number of variables.
COMPUTE Condition = SUM (MEAN(ID EQ 1,VAR1 EQ 2,VAR2 EQ 3,VAR3 EQ 4) EQ 1, NMISS(ID EQ 1,VAR1 EQ 2,VAR2 EQ 3,VAR3 EQ 4) EQ 0) EQ 2. EXECUTE . Note : I am unsure whether riya would like the above formula as it doesn't meet the purpose of eliminating AND ;)) |
Administrator
|
Ujjawal, note that you don't need the equality tests in your NMISS. I.e., you could shorten it to:
COMPUTE Condition = SUM (MEAN(ID EQ 1,VAR1 EQ 2,VAR2 EQ 3,VAR3 EQ 4) EQ 1, NMISS(ID,VAR1,VAR23,VAR3) EQ 0) EQ 2. But as you suggest, this is not really gaining anything over Riya's original approach: COMPUTE Condition = NMISS(ID,VAR1,VAR2,VAR3) EQ 0 and (ID EQ 1) and (Var1 EQ 2) and (var2 EQ 3) and (var3 EQ 4). In fact, her original method uses fewer keystrokes, and is probably more transparent for most users. If one needed to do something like this for a LOT of variables, a VECTOR & LOOP method could be used, with an early exit from the LOOP as soon as the condition became true. It would not surprise me if there is an example of something like that in the archives of this list. Cheers, Bruce
--
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 |