Conditional Coding

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Conditional Coding

james.moffitt

I’m trying to create a new variable (named newvar) that is coded according to the values of three existing variables. Here are the coding conditions for newvar:

 

Condition 1: If Q1, Q2 and Q3 all contain a 9 or a 10, newvar should equal 4.

Condition 1: If Q1 or  Q2 or Q3 contain a 1 or a 2 or a 3, newvar should equal 1.

 

The following syntax accomplishes the coding of the first two conditions although in an admittedly very inefficient manner:

 

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .

COMPUTE newvar=4.

ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .

COMPUTE newvar=1.

END IF.

EXECUTE.

 

My problems lie with the remaining conditions.

 

Condition 3 is: If Q1, Q2 and Q3 all contain a 7, 8, 9 or a 10, but do not each contain a 9 or a 10 (thereby satisfying condition 1), newvar should equal 3.

 

Condition 4 is: If Q1, Q2 and Q3 all contain a value of 1 through 10, but do not satisfy any of the preceding conditions, newvar should equal 2.

 

Condition 5 is: If any of Q1, Q2, or Q3 is system missing or user missing, newvar should be system missing.

 

How would I write the syntax to satisfy all coding conditions?

 

I’m using SPSS 15.0.

 

Thanks in advance.

 

Reply | Threaded
Open this post in threaded view
|

Re: Conditional Coding

Melissa Ives

You are using the DO IF …ELSE IF syntax which in essence says …if any records do not match the do if then check the else if to see if they match that, if any still don’t match then do the next else if…

So keep going with the logic.  Something like below (untested).  Based on what you noted, newvar=3 only happens if newvar is not 4, but values of 2 occur if newvar is not 1, 3 or 4), hence the order.

The code will set newvar to 1 if only one answer is available for Q1, Q2, Q3 that meets that criteria (due to the ‘or’).

 

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .

  COMPUTE newvar=4.

ELSE IF ((Q1 GE 7 AND Q1 LE 10) AND (Q2 GE 7 AND Q2 LE 10) AND (Q3 GE 7 AND Q3 LE 10)).

  COMPUTE newvar=3.

ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .

  COMPUTE newvar=1.

ELSE IF (NVALID(Q1,Q2,Q3)=3.

  COMPUTE newvar=2.

END IF.

IF ((NVALID(Q1,Q2,Q3)<3) newvar=$sysmis.

 

Melissa

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Thursday, July 26, 2012 11:16 AM
To: [hidden email]
Subject: [SPSSX-L] Conditional Coding

 

I’m trying to create a new variable (named newvar) that is coded according to the values of three existing variables. Here are the coding conditions for newvar:

 

Condition 1: If Q1, Q2 and Q3 all contain a 9 or a 10, newvar should equal 4.

Condition 1: If Q1 or  Q2 or Q3 contain a 1 or a 2 or a 3, newvar should equal 1.

 

The following syntax accomplishes the coding of the first two conditions although in an admittedly very inefficient manner:

 

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .

COMPUTE newvar=4.

ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .

COMPUTE newvar=1.

END IF.

EXECUTE.

 

My problems lie with the remaining conditions.

 

Condition 3 is: If Q1, Q2 and Q3 all contain a 7, 8, 9 or a 10, but do not each contain a 9 or a 10 (thereby satisfying condition 1), newvar should equal 3.

 

Condition 4 is: If Q1, Q2 and Q3 all contain a value of 1 through 10, but do not satisfy any of the preceding conditions, newvar should equal 2.

 

Condition 5 is: If any of Q1, Q2, or Q3 is system missing or user missing, newvar should be system missing.

 

How would I write the syntax to satisfy all coding conditions?

 

I’m using SPSS 15.0.

 

Thanks in advance.

 



PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.
Reply | Threaded
Open this post in threaded view
|

Re: Conditional Coding

David Marso
Administrator
Here is another method.
I personally find nested DO IF statements to be somewhat messy.
RANGE and ANY are useful here as well.
DATA LIST FREE / q1 q2 q3.
begin data
9 9 9 9 9 10 9 9 1 9 9 4 1 2 3 4 4 4 7 8 9 1 2 .
end data.

COMPUTE newvar=(SUM(q1,q2,q3) GE 27) * 4 + ANY(1,ANY(1,Q1,Q2,Q3),ANY(2,Q1,Q2,Q3),ANY(3,Q1,Q2,Q3)).
IF (newvar EQ 0) Newvar = RANGE(q1,7,10)*RANGE(q2,7,10)*RANGE(q3,7,10)*3 .
IF (newvar EQ 0) Newvar=2.
IF NMISS(q1,q2,q3) GT 0 newvar=$SYSMIS.
LIST.

      Q1       Q2       Q3   NEWVAR

9  9  9  4
9  9  10 4
9  9  1  1
9  9  4  2  
1  2  3  1
4  4  4  2
7  8  9  3
1  2  .  .


Number of cases read:  8    Number of cases listed:  8

Melissa Ives wrote
You are using the DO IF ...ELSE IF syntax which in essence says ...if any records do not match the do if then check the else if to see if they match that, if any still don't match then do the next else if...
So keep going with the logic.  Something like below (untested).  Based on what you noted, newvar=3 only happens if newvar is not 4, but values of 2 occur if newvar is not 1, 3 or 4), hence the order.
The code will set newvar to 1 if only one answer is available for Q1, Q2, Q3 that meets that criteria (due to the 'or').

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .
  COMPUTE newvar=4.
ELSE IF ((Q1 GE 7 AND Q1 LE 10) AND (Q2 GE 7 AND Q2 LE 10) AND (Q3 GE 7 AND Q3 LE 10)).
  COMPUTE newvar=3.
ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .
  COMPUTE newvar=1.
ELSE IF (NVALID(Q1,Q2,Q3)=3.
  COMPUTE newvar=2.
END IF.
IF ((NVALID(Q1,Q2,Q3)<3) newvar=$sysmis.

Melissa

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Thursday, July 26, 2012 11:16 AM
To: [hidden email]
Subject: [SPSSX-L] Conditional Coding

I'm trying to create a new variable (named newvar) that is coded according to the values of three existing variables. Here are the coding conditions for newvar:

Condition 1: If Q1, Q2 and Q3 all contain a 9 or a 10, newvar should equal 4.
Condition 1: If Q1 or  Q2 or Q3 contain a 1 or a 2 or a 3, newvar should equal 1.

The following syntax accomplishes the coding of the first two conditions although in an admittedly very inefficient manner:

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .
COMPUTE newvar=4.
ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .
COMPUTE newvar=1.
END IF.
EXECUTE.

My problems lie with the remaining conditions.

Condition 3 is: If Q1, Q2 and Q3 all contain a 7, 8, 9 or a 10, but do not each contain a 9 or a 10 (thereby satisfying condition 1), newvar should equal 3.

Condition 4 is: If Q1, Q2 and Q3 all contain a value of 1 through 10, but do not satisfy any of the preceding conditions, newvar should equal 2.

Condition 5 is: If any of Q1, Q2, or Q3 is system missing or user missing, newvar should be system missing.

How would I write the syntax to satisfy all coding conditions?

I'm using SPSS 15.0.

Thanks in advance.


________________________________
PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.
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?"
Reply | Threaded
Open this post in threaded view
|

Re: Conditional Coding

james.moffitt
In reply to this post by Melissa Ives

Thank you, Melissa, for the code and the explanation. I had some trouble getting the code to execute, but I think your explanation will put me on the right track.

 

 

From: Melissa Ives [mailto:[hidden email]]
Sent: Thursday, July 26, 2012 12:10 PM
To: Moffitt, James (Legal); [hidden email]
Subject: RE: Conditional Coding

 

You are using the DO IF …ELSE IF syntax which in essence says …if any records do not match the do if then check the else if to see if they match that, if any still don’t match then do the next else if…

So keep going with the logic.  Something like below (untested).  Based on what you noted, newvar=3 only happens if newvar is not 4, but values of 2 occur if newvar is not 1, 3 or 4), hence the order.

The code will set newvar to 1 if only one answer is available for Q1, Q2, Q3 that meets that criteria (due to the ‘or’).

 

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .

  COMPUTE newvar=4.

ELSE IF ((Q1 GE 7 AND Q1 LE 10) AND (Q2 GE 7 AND Q2 LE 10) AND (Q3 GE 7 AND Q3 LE 10)).

  COMPUTE newvar=3.

ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .

  COMPUTE newvar=1.

ELSE IF (NVALID(Q1,Q2,Q3)=3.

  COMPUTE newvar=2.

END IF.

IF ((NVALID(Q1,Q2,Q3)<3) newvar=$sysmis.

 

Melissa

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Thursday, July 26, 2012 11:16 AM
To: [hidden email]
Subject: [SPSSX-L] Conditional Coding

 

I’m trying to create a new variable (named newvar) that is coded according to the values of three existing variables. Here are the coding conditions for newvar:

 

Condition 1: If Q1, Q2 and Q3 all contain a 9 or a 10, newvar should equal 4.

Condition 1: If Q1 or  Q2 or Q3 contain a 1 or a 2 or a 3, newvar should equal 1.

 

The following syntax accomplishes the coding of the first two conditions although in an admittedly very inefficient manner:

 

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .

COMPUTE newvar=4.

ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .

COMPUTE newvar=1.

END IF.

EXECUTE.

 

My problems lie with the remaining conditions.

 

Condition 3 is: If Q1, Q2 and Q3 all contain a 7, 8, 9 or a 10, but do not each contain a 9 or a 10 (thereby satisfying condition 1), newvar should equal 3.

 

Condition 4 is: If Q1, Q2 and Q3 all contain a value of 1 through 10, but do not satisfy any of the preceding conditions, newvar should equal 2.

 

Condition 5 is: If any of Q1, Q2, or Q3 is system missing or user missing, newvar should be system missing.

 

How would I write the syntax to satisfy all coding conditions?

 

I’m using SPSS 15.0.

 

Thanks in advance.

 

 


PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.

Reply | Threaded
Open this post in threaded view
|

Re: Conditional Coding

james.moffitt
In reply to this post by David Marso
Thanks, David. This seems to run perfectly. I really appreciate your help.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of David Marso
Sent: Thursday, July 26, 2012 12:46 PM
To: [hidden email]
Subject: Re: Conditional Coding

Here is another method.
I personally find nested DO IF statements to be somewhat messy.
RANGE and ANY are useful here as well.
DATA LIST FREE / q1 q2 q3.
begin data
9 9 9 9 9 10 9 9 1 9 9 4 1 2 3 4 4 4 7 8 9 1 2 .
end data.

COMPUTE newvar=(SUM(q1,q2,q3) GE 27) * 4 +
ANY(1,ANY(1,Q1,Q2,Q3),ANY(2,Q1,Q2,Q3),ANY(3,Q1,Q2,Q3)).
IF (newvar EQ 0) Newvar = RANGE(q1,7,10)*RANGE(q2,7,10)*RANGE(q3,7,10)*3 .
IF (newvar EQ 0) Newvar=2.
IF NMISS(q1,q2,q3) GT 0 newvar=$SYSMIS.
LIST.

      Q1       Q2       Q3   NEWVAR

9  9  9  4
9  9  10 4
9  9  1  1
9  9  4  2
1  2  3  1
4  4  4  2
7  8  9  3
1  2  .  .


Number of cases read:  8    Number of cases listed:  8


Melissa Ives wrote

>
> You are using the DO IF ...ELSE IF syntax which in essence says ...if any
> records do not match the do if then check the else if to see if they match
> that, if any still don't match then do the next else if...
> So keep going with the logic.  Something like below (untested).  Based on
> what you noted, newvar=3 only happens if newvar is not 4, but values of 2
> occur if newvar is not 1, 3 or 4), hence the order.
> The code will set newvar to 1 if only one answer is available for Q1, Q2,
> Q3 that meets that criteria (due to the 'or').
>
> DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .
>   COMPUTE newvar=4.
> ELSE IF ((Q1 GE 7 AND Q1 LE 10) AND (Q2 GE 7 AND Q2 LE 10) AND (Q3 GE 7
> AND Q3 LE 10)).
>   COMPUTE newvar=3.
> ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2
> or Q3=3)) .
>   COMPUTE newvar=1.
> ELSE IF (NVALID(Q1,Q2,Q3)=3.
>   COMPUTE newvar=2.
> END IF.
> IF ((NVALID(Q1,Q2,Q3)<3) newvar=$sysmis.
>
> Melissa
>
> From: SPSSX(r) Discussion [mailto:SPSSX-L@.UGA] On Behalf Of
> james.moffitt@
> Sent: Thursday, July 26, 2012 11:16 AM
> To: SPSSX-L@.UGA
> Subject: [SPSSX-L] Conditional Coding
>
> I'm trying to create a new variable (named newvar) that is coded according
> to the values of three existing variables. Here are the coding conditions
> for newvar:
>
> Condition 1: If Q1, Q2 and Q3 all contain a 9 or a 10, newvar should equal
> 4.
> Condition 1: If Q1 or  Q2 or Q3 contain a 1 or a 2 or a 3, newvar should
> equal 1.
>
> The following syntax accomplishes the coding of the first two conditions
> although in an admittedly very inefficient manner:
>
> DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .
> COMPUTE newvar=4.
> ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2
> or Q3=3)) .
> COMPUTE newvar=1.
> END IF.
> EXECUTE.
>
> My problems lie with the remaining conditions.
>
> Condition 3 is: If Q1, Q2 and Q3 all contain a 7, 8, 9 or a 10, but do not
> each contain a 9 or a 10 (thereby satisfying condition 1), newvar should
> equal 3.
>
> Condition 4 is: If Q1, Q2 and Q3 all contain a value of 1 through 10, but
> do not satisfy any of the preceding conditions, newvar should equal 2.
>
> Condition 5 is: If any of Q1, Q2, or Q3 is system missing or user missing,
> newvar should be system missing.
>
> How would I write the syntax to satisfy all coding conditions?
>
> I'm using SPSS 15.0.
>
> Thanks in advance.
>
>
> ________________________________
> PRIVILEGED AND CONFIDENTIAL INFORMATION
> This transmittal and any attachments may contain PRIVILEGED AND
> CONFIDENTIAL information and is intended only for the use of the
> addressee. If you are not the designated recipient, or an employee
> or agent authorized to deliver such transmittals to the designated
> recipient, you are hereby notified that any dissemination,
> copying or publication of this transmittal is strictly prohibited. If
> you have received this transmittal in error, please notify us
> immediately by replying to the sender and delete this copy from your
> system. You may also call us at (309) 827-6026 for assistance.
>




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Conditional-Coding-tp5714470p5714473.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
Reply | Threaded
Open this post in threaded view
|

Re: Conditional Coding

Art Kendall
In reply to this post by james.moffitt
condition 5 has a value assigned sysmis.
However the value is NOT (missing because the machine cannot follow your instructions)  it has a missing value because the user said to assign a missing value.   it is better to assign a value the is outside the target set of valid values.  -1 would do.

double check that I have matched your logic.

* make up some data.
new file.
input program.
   vector q (3,f2).
   loop id = 1 to 250.
      loop #p = 1 to 3.
         compute q(#p) = rnd(rv.uniform(-.500001, 10.49999)).
      end loop.
      end case.
   end loop.
   end file.
end input program.

*USE THE SYNTAX BELOW WITH YOUR DATA.
string testcoding (a6).
compute testcoding = concat(string(q1,n2),string(q2,n2),string(q3,n2)).
EXECUTE.
missing values q1 to q3 (0).
DO IF NVALID(Q1,Q2,Q3) ne 3.
   compute newvar = -1.
ELSE IF ((Q1 GE 9) and (Q2 GE 9) and (Q3 GE 9)) .
   COMPUTE newvar =  4.
ElSE IF (ANY(Q1,1,2,3) OR ANY(Q2,1,2,3) OR ANY(Q3,1,2,3) ) .
   COMPUTE newvar =  1.
ELSE IF (Q1 GE 9) and (Q2  GE 9 ) and (Q3 ge 9) .
   COMPUTE newvar =  4.
ELSE IF ((Q1 GE 7) AND (Q2 GE 7 ) AND (Q3 GE 7 )).
   COMPUTE newvar =  3.
ELSE IF ANY(Q1,1,2,3) OR ANY(Q2,1,2,3) OR ANY(Q3,1,2,3) .
   COMPUTE newvar =  1.
ELSE IF NVALID(Q1,Q2,Q3)=3.
   COMPUTE newvar =  2.
ELSE.
   compute newvar =  -2.
END IF.
MISSING VALUES newvar(lo thru -1).
value labels newvar
   -1 'all 3 missing'
   -2 'fell through conditions'.

crosstabs tables= testcoding by newvar
 /MISSING=INCLUDE.

Art Kendall
Social Research Consultants
On 7/26/2012 12:16 PM, [hidden email] wrote:

I’m trying to create a new variable (named newvar) that is coded according to the values of three existing variables. Here are the coding conditions for newvar:

 

Condition 1: If Q1, Q2 and Q3 all contain a 9 or a 10, newvar should equal 4.

Condition 1: If Q1 or  Q2 or Q3 contain a 1 or a 2 or a 3, newvar should equal 1.

 

The following syntax accomplishes the coding of the first two conditions although in an admittedly very inefficient manner:

 

DO IF ((Q1=9 or Q1=10) and (Q2=9 or Q2=10) and (Q3=9 or Q3=10)) .

COMPUTE newvar=4.

ELSE IF ((Q1=1 or Q1=2 or Q1=3) or (Q2=1 or Q2=2 or Q2=3) or (Q3=1 or Q3=2 or Q3=3)) .

COMPUTE newvar=1.

END IF.

EXECUTE.

 

My problems lie with the remaining conditions.

 

Condition 3 is: If Q1, Q2 and Q3 all contain a 7, 8, 9 or a 10, but do not each contain a 9 or a 10 (thereby satisfying condition 1), newvar should equal 3.

 

Condition 4 is: If Q1, Q2 and Q3 all contain a value of 1 through 10, but do not satisfy any of the preceding conditions, newvar should equal 2.

 

Condition 5 is: If any of Q1, Q2, or Q3 is system missing or user missing, newvar should be system missing.

 

How would I write the syntax to satisfy all coding conditions?

 

I’m using SPSS 15.0.

 

Thanks in advance.

 


===================== 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
Art Kendall
Social Research Consultants