HELP NEEDED: USING COMPUTE FOR CONDITIONAL TRANSFORMATION

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

annabusiness
Posts: 2
Joined: Sat Apr 16, 2011 11:20 am

HELP NEEDED: USING COMPUTE FOR CONDITIONAL TRANSFORMATION

Postby annabusiness » Wed Apr 18, 2012 2:06 pm

Hi,

I have a number of variables coded 1/0 which indicate whether a respondent was involved in sub-categories of local government. I now want to combine these into a smaller number of categories using the compute command. Basically I want to say if any of a number of variables=1 then the new variable should equal 1, otherwise it should equal 0. I wrote the syntax below to do this, but keep getting back a bunch of missing data.

Can anybody help me?

The syntax I tried was:
DO IF Q8_RC_pc_sc EQ 1.
COMPUTE Q8_inv_chiefg=1.
ELSE IF Q8_RC_kapr EQ 1.
COMPUTE Q8_inv_chiefg=1.
ELSE IF Q8_RC_subc EQ 1.
COMPUTE Q8_inv_chiefg=1.
ELSE IF Q8_RC_lcourt EQ 1.
COMPUTE Q8_inv_chiefg=1.
ELSE IF Q8_RC_cc EQ 1.
COMPUTE Q8_inv_chiefg=1.
ELSE.
COMPUTE Q8_inv_chiefg=0.
END IF.
apeape
Posts: 56
Joined: Mon May 02, 2011 6:07 pm

Re: HELP NEEDED: USING COMPUTE FOR CONDITIONAL TRANSFORMATIO

Postby apeape » Wed Apr 18, 2012 2:47 pm

If your variables are contiguous, the following should get you the result you want:

Code: Select all

compute Q8_inv_chiefg = 0.
if (any(1, Q8_RC_pc_sc to Q8_RC_cc)) Q8_inv_chiefg = 1.
exe.
If they are not contiguous, then this other way should suffice:

Code: Select all

compute Q8_inv_chiefg = 0.
IF (Q8_RC_pc_sc  or Q8_RC_kapr or Q8_RC_subc or Q8_RC_lcourt or Q8_RC_cc EQ 1) Q8_inv_chiefg=1.
exe.
*Untested.
annabusiness
Posts: 2
Joined: Sat Apr 16, 2011 11:20 am

Re: HELP NEEDED: USING COMPUTE FOR CONDITIONAL TRANSFORMATIO

Postby annabusiness » Wed Apr 18, 2012 8:04 pm

Thank you so much--this worked.

One further question--is there a simple way for me to combine a number of variables coded 1/0 into a single variable in which each of the positive values takes on a different number?
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: HELP NEEDED: USING COMPUTE FOR CONDITIONAL TRANSFORMATIO

Postby Penguin_Knight » Wed Apr 18, 2012 8:52 pm

annabusiness wrote:Thank you so much--this worked.

One further question--is there a simple way for me to combine a number of variables coded 1/0 into a single variable in which each of the positive values takes on a different number?
COMPUTE combinedVar = SUM(Q8_RC_pc_sc * 10000, Q8_RC_kapr * 1000, Q8_RC_subc * 100, Q8_RC_lcourt * 10, Q8_RC_cc) .
EXECUTE .
FORMAT combinedVar (n5.0) .
EXECUTE .

Who is online

Users browsing this forum: No registered users and 1 guest

cron