Is there an easy way to set bits in a an SPSS variable? I’m trying to create a variable based on the combinations of other (dichotomous) variables. The way that seems logically easy to me is to set a bit for each of the variables, and then the output variable will automatically represent the correct combination. Thus (pseudo code) IF (VarA) COMPUTE Comb = Comb AND 1. IF (VarB) COMPUTE Comb = Comb AND 2. IF (VarC) COMPUTE Comb = Comb AND 4. Etc. But I can’t figure out how to do this. Instead, I’m using all the combinations with individual IF statements which works for a small number, but isn’t very scalable and is easy to make mistakes. COMPUTE Comb=$SYSMIS. IF ((VarA=1) AND (VarB=1) AND (VarC=1)) Comb=7. IF ((VarA=1) AND (VarB=1) AND (VarC=0)) Comb=6. IF ((VarA=1) AND (VarB=0) AND (VarC=1)) Comb=5. IF ((VarA=1) AND (VarB=0) AND (VarC=0)) Comb=4. IF ((VarA=0) AND (VarB=1) AND (VarC=1)) Comb=3. IF ((VarA=0) AND (VarB=1) AND (VarC=0)) Comb=2. IF ((VarA=0) AND (VarB=0) AND (VarC=1)) Comb=1. IF ((VarA=0) AND (VarB=0) AND (VarC=0)) Comb=0. EXECUTE . As usual with my questions, this feels like something that should be obvious. Thanks in advance. Mike _________________________________________________________________________ |
It all depends on what you are really going
to do with this packed form, but typically one would combine these as powers
of 2. E.g.,
compute comb = a * 2**0 + b * 2**1 + c * 2**2 + .... SPSS does not provide direct operations on bits as suggested in your pseudocode, but I think you meant OR there rather than AND. If you really want to do bit packing, you could use the packDummies and extractDummies function in the extendedTransforms.py module available from the SPSS Community website in the Python modules collection. These could conveniently be used in a transformation with the SPSSINC TRANS extension command. HTH, Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: Mike Pritchard <[hidden email]> To: [hidden email] Date: 04/01/2012 12:37 PM Subject: [SPSSX-L] Setting bits in SPSS variable Sent by: "SPSSX(r) Discussion" <[hidden email]> Is there an easy way to set bits in a an SPSS variable? I’m trying to create a variable based on the combinations of other (dichotomous) variables. The way that seems logically easy to me is to set a bit for each of the variables, and then the output variable will automatically represent the correct combination. Thus (pseudo code) IF (VarA) COMPUTE Comb = Comb AND 1. IF (VarB) COMPUTE Comb = Comb AND 2. IF (VarC) COMPUTE Comb = Comb AND 4. Etc. But I can’t figure out how to do this. Instead, I’m using all the combinations with individual IF statements which works for a small number, but isn’t very scalable and is easy to make mistakes. COMPUTE Comb=$SYSMIS. IF ((VarA=1) AND (VarB=1) AND (VarC=1)) Comb=7. IF ((VarA=1) AND (VarB=1) AND (VarC=0)) Comb=6. IF ((VarA=1) AND (VarB=0) AND (VarC=1)) Comb=5. IF ((VarA=1) AND (VarB=0) AND (VarC=0)) Comb=4. IF ((VarA=0) AND (VarB=1) AND (VarC=1)) Comb=3. IF ((VarA=0) AND (VarB=1) AND (VarC=0)) Comb=2. IF ((VarA=0) AND (VarB=0) AND (VarC=1)) Comb=1. IF ((VarA=0) AND (VarB=0) AND (VarC=0)) Comb=0. EXECUTE . As usual with my questions, this feels like something that should be obvious. Thanks in advance. Mike _________________________________________________________________________ Mike Pritchard | mikep@... | 5 Circles Research | 425-444-3410 (c) | 425-968-3883 (o) Research to help companies build products that people buy |
In reply to this post by Mike Pritchard
Mike, There a couple of different ways of doing something like what you want. Assuming VarA-VarC has value 0,1. Using strings. String comb(a3). Compute comb=concat(string(vara,f1.0), string(varb,f1.0), string(varc,f1.0)). Using numeric Compute comb=100*vara+10*varb+varc. Gene Maguin From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Mike Pritchard Is there an easy way to set bits in a an SPSS variable? I’m trying to create a variable based on the combinations of other (dichotomous) variables. The way that seems logically easy to me is to set a bit for each of the variables, and then the output variable will automatically represent the correct combination. Thus (pseudo code) IF (VarA) COMPUTE Comb = Comb AND 1. IF (VarB) COMPUTE Comb = Comb AND 2. IF (VarC) COMPUTE Comb = Comb AND 4. Etc. But I can’t figure out how to do this. Instead, I’m using all the combinations with individual IF statements which works for a small number, but isn’t very scalable and is easy to make mistakes. COMPUTE Comb=$SYSMIS. IF ((VarA=1) AND (VarB=1) AND (VarC=1)) Comb=7. IF ((VarA=1) AND (VarB=1) AND (VarC=0)) Comb=6. IF ((VarA=1) AND (VarB=0) AND (VarC=1)) Comb=5. IF ((VarA=1) AND (VarB=0) AND (VarC=0)) Comb=4. IF ((VarA=0) AND (VarB=1) AND (VarC=1)) Comb=3. IF ((VarA=0) AND (VarB=1) AND (VarC=0)) Comb=2. IF ((VarA=0) AND (VarB=0) AND (VarC=1)) Comb=1. IF ((VarA=0) AND (VarB=0) AND (VarC=0)) Comb=0. EXECUTE . As usual with my questions, this feels like something that should be obvious. Thanks in advance. Mike _________________________________________________________________________ |
In reply to this post by Jon K Peck
Jon, Gene, Thanks for the suggestions. I think I must have had a brain spasm, because I forgot that each response would only be evaluated once – hence your approaches were what I should have come up with. Jon, I probably used poor pseudo code for setting the bits – based on the operators in SPSS – but you are correct from other languages. I just created a new variable using the simpler syntax to confirm that the same results are generated. Thanks! Mike _________________________________________________________________________ The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. From: Jon K Peck [mailto:[hidden email]] It all depends on what you are really going to do with this packed form, but typically one would combine these as powers of 2. E.g., Mike, There a couple of different ways of doing something like what you want. Assuming VarA-VarC has value 0,1. Using strings. String comb(a3). Compute comb=concat(string(vara,f1.0), string(varb,f1.0), string(varc,f1.0)). Using numeric Compute comb=100*vara+10*varb+varc. Gene Maguin
|
Free forum by Nabble | Edit this page |