Hi All
Is there a way I can simplify my syntax so that it computes if '2 or more of the following'' conditions are met, instead of writing out every combination possible? If Q49=0&Q59=1&((Q54>1&Q53>1)OR(Q54>1&Q55>0)OR(Q54>1&Q56>0)OR(Q54>1&Q57>0)OR(Q54>1&Q52>1)OR(Q53>1&Q55>0)OR(Q53>1&Q56>0)OR(Q53>1&Q57>0)OR(Q53>1&Q52>1)OR(Q56>0&Q57>1)OR(Q56>0&Q52>1)OR(Q56>0&Q55>0)OR(Q57>0&Q52>1)OR(Q57>0&Q55>0))NOT(Q41>2&(Q43=0ORQ43=2)&Q45=1&((Q46>0&(Q47>0ORQ48>0))OR(Q46>0&(Q49>0ORQ50>0))OR((Q47>0ORQ48>0)& (Q49>0ORQ50>0)))) C3=1 THank you Ecushla |
Administrator
|
Does that code even run? No!
Are my eyes bleeding from looking at it? Yes! Please describe WHAT you are trying to do here (preferably in English)? Readability in code is something to strive for (Especially if you expect others to help you fix it)!
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 ECL
Apologies, ALL syntax looks unreadable to me and I was not aware that there is a particular way of doing it. Hope this reads better?
Basically, I want to know if there is an easier way of asking for 'any two of the following combinations' where you can choose from Q54>1, Q53>1,Q55>0, Q56>0, etc without listing all the possible combinations which I have done below. The code runs, but I figure there must be an easier way. I'm not wanting the ANY command, but rather ''ANY 2 or more" if there is such a thing. IF Q49=0 & Q59=1 AND ( (Q54>1&Q53>1) OR (Q54>1&Q55>0) OR (Q54>1&Q56>0) OR (Q54>1&Q57>0) OR (Q54>1&Q52>1) OR (Q53>1&Q55>0) OR (Q53>1&Q56>0) OR (Q53>1&Q57>0) OR (Q53>1&Q52>1) OR (Q56>0&Q57>1) OR (Q56>0&Q52>1) OR (Q56>0&Q55>0) OR (Q57>0&Q52>1) OR (Q57>0&Q55>0) ) AND NOT(C1=1) C3=1. Execute. Thanks |
Administrator
|
Without rewriting your code for you I will merely note/point out that.
A & B OR A & C OR A & D may be refactored as A & ( B OR C OR D ) ie A & ANY(1,B,C,D) also A > 0 OR B > 0 can be sorted as SUM(A,B) > 0 OTOH: Does simply SUM(Q54 > 1, Q53 > 1,Q55 > 0, Q56 > 0, etc) GE 2 fit the bill? Note that the inequality evaluates to either false,true or missing =0, 1 or MISSING SUM(missing, x)=x SUM(missing,missing)=missing
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?" |
One other useful way to refactor the code is sometimes to flip the conditionals; that is initialize the variable to 1 and then evaluate all of the arguments and change it to 0 if the sub-arguments are not met.
Note David's suggestion of the SUM's is presuming the variables can not take negative values (which I am unsure if that was stated somewhere in the prior threads). For such a large table of assignments though having a physically separate look up table is sometimes in order. So then all you need is a MATCH FILES, editing future scenarios tends to be simpler, and just seeing how the code assigns categories is simpler then viewing syntax. |
In reply to this post by ECL
It would help all
readers of your syntax if you were to use more lines to present
it.
ANY would work something like this (untested) but would be a little superfluous in this instance. [I did not try to help the visualization of part2.] Compute part1 = If Q49 EQ 0 AND Q59 EQ 1 AND any( ( (Q54 GT 1 AND Q53 GT 1) OR (Q54 GT 1 AND Q55 GT 0) OR (Q54 GT 1 AND Q56 GT 0) OR (Q54 GT 1 AND Q57 GT 0) OR (Q54 GT 1 AND Q52 GT 1) OR (Q53 GT 1 AND Q55 GT 0) OR (Q53 GT 1 AND Q56 GT 0) OR (Q53 GT 1 AND Q57 GT 0) OR (Q53 GT 1 AND Q52 GT 1) OR (Q56 GT 0 AND Q57 GT 1) OR (Q56 GT 0 AND Q52 GT 1) OR (Q56 GT 0 AND Q55 GT 0) OR (Q57 GT 0 AND Q52 GT 1) OR (Q57 GT 0 AND Q55 GT 0) ), 1). Compute part 2 = NOT(Q41 GT 2 AND (Q43 EQ 0 OR Q43 EQ 2) AND Q45 EQ 1 AND ((Q46 GT 0 AND (Q47 GT 0 OR Q48 GT 0)) OR (Q46 GT 0 AND (Q49 GT 0 OR Q50 GT 0)) OR ((Q47 GT 0 OR Q48 GT 0) AND (Q49 GT 0 OR Q50 GT 0)))). Conpute c3 = part1 and part2. Art Kendall Social Research ConsultantsOn 5/7/2014 9:27 PM, ECL [via SPSSX Discussion] wrote: Hi All
Art Kendall
Social Research Consultants |
In reply to this post by ECL
At 09:27 PM 5/7/2014, ECL wrote:
>Is there a way I can simplify my syntax so that it computes if '2 or >more of the following'' conditions are met, instead of writing out >every combination possible? I unrolled your statement. You seem to mean two or more of the following conditions: Q52>1 Q53>1 Q54>1 Q55>0 Q56>0 Q57>0 For this, try SUM(Q52>1,Q53>1,Q54>1,Q55>0,Q56>0,Q57>0) GE 2 Below is your statement as you had it, except for adding line breaks and spaces. It has at least three syntax errors: on the third line, and on the last line, you shouldn't have the "*" characters. And the "NOT" needs to be preceded by a logical operator -- but I can't make out what you might be trying to do from the word "NOT" on: If Q49=0 & Q59=1 *& ( (Q54>1&Q53>1) OR(Q54>1&Q55>0) OR(Q54>1&Q56>0) OR(Q54>1&Q57>0) OR(Q54>1&Q52>1) OR(Q53>1&Q55>0) OR(Q53>1&Q56>0) OR(Q53>1&Q57>0) OR(Q53>1&Q52>1) OR(Q56>0&Q57>1) OR(Q56>0&Q52>1) OR(Q56>0&Q55>0) OR(Q57>0&Q52>1) OR(Q57>0&Q55>0)) NOT ( Q41>2 & (Q43=0 OR Q43=2) & Q45=1 & ((Q46>0&(Q47>0 OR Q48>0)) OR(Q46>0&(Q49>0 OR Q50>0)) OR((Q47>0 OR Q48>0)&(Q49>0 OR Q50>0)))) *C3=1. ===================== 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 ECL
It would help all
readers of your syntax if you were to use more lines to present
it.
vertical alignment is an old programmers trick to see parallel logic. ANY would work something like this (untested) but would be a little superfluous in this instance. [I did not try to help the visualization of part2.] Compute part1 = If Q49 EQ 0 AND Q59 EQ 1 AND any( ( (Q54 GT 1 AND Q53 GT 1) OR (Q54 GT 1 AND Q55 GT 0) OR (Q54 GT 1 AND Q56 GT 0) OR (Q54 GT 1 AND Q57 GT 0) OR (Q54 GT 1 AND Q52 GT 1) OR (Q53 GT 1 AND Q55 GT 0) OR (Q53 GT 1 AND Q56 GT 0) OR (Q53 GT 1 AND Q57 GT 0) OR (Q53 GT 1 AND Q52 GT 1) OR (Q56 GT 0 AND Q57 GT 1) OR (Q56 GT 0 AND Q52 GT 1) OR (Q56 GT 0 AND Q55 GT 0) OR (Q57 GT 0 AND Q52 GT 1) OR (Q57 GT 0 AND Q55 GT 0) ), 1). Compute part 2 = NOT(Q41 GT 2 AND (Q43 EQ 0 OR Q43 EQ 2) AND Q45 EQ 1 AND ((Q46 GT 0 AND (Q47 GT 0 OR Q48 GT 0)) OR (Q46 GT 0 AND (Q49 GT 0 OR Q50 GT 0)) OR ((Q47 GT 0 OR Q48 GT 0) AND (Q49 GT 0 OR Q50 GT 0)))). Conpute c3 = part1 and part2. Art Kendall Social Research ConsultantsOn 5/7/2014 9:27 PM, ECL [via SPSSX Discussion] wrote: Hi All
Art Kendall
Social Research Consultants |
In reply to this post by ECL
Four things that would
help make you syntax more readable.
(1) use vertical alignment for parallel segment of statements as you did in this post. (2) use indentation (3) break complex logic into parts compute part1 = list of conditions. compute part2 = another list of conditions compute flag = part1 and part2 (4) use the conventional operators (gt lt, ge, etc) rather than the symbolic operators (>, >, >=, etc). especially reserve the assignment operator (=) for assignment and not for logical AND. Art Kendall Social Research ConsultantsOn 5/8/2014 1:11 AM, ECL [via SPSSX Discussion] wrote: Apologies, ALL syntax looks unreadable to me and I was not aware that there is a particular way of doing it. Hope this reads better?
Art Kendall
Social Research Consultants |
Free forum by Nabble | Edit this page |