Larissa
Larissa

Reputation: 83

SPSS recoding variables data from multiple variables into boolean variables

I have 26 variables and each of them contain numbers ranging from 1 to 61. I want for each case of 1, each case of 2 etc. the number 1 in a new variable. If there is no 1, the variable should contain 2. So 26 variables with data like: 1 15 28 39 46 1 12 etc.

And I want 61 variables with: 1 2 1 2 2 1 etc.

I have been reading about creating vectors, loops, do if's etc but I can't find the right way to code it. What I have done is just creating 61 variables and writing

do if V1=1 or V2=1 or (etc until V26).

recode newV1=1.

end if.

exe.

**repeat this for all 61 variables.

recode newV1 to newV61(missing=2).

So this is a lot of code and quite a detour from what I imagine it could be.

Anyone who can help me out with this one? Your help is much appreciated!

Upvotes: 3

Views: 694

Answers (3)

eli-k
eli-k

Reputation: 11310

This should do it:

do repeat NewV=NewV1 to NewV61/vl=1 to 61.
  compute NewV=any(vl,v1 to v26).
end repeat.

EXPLANATION: This syntax will go through values 1 to 61, for each one checking whether any of the variables v1 to v26 has that value. If any of them do, the right NewV will receive the value of 1. If none of them do, the right NewV will receive the value of 0. Just make sure v1 to v26 are consecutively ordered in the file. if not, then change to:

compute NewV=any(vl,v1, v2, v3, v4 ..... v26).

Upvotes: 1

Andy W
Andy W

Reputation: 5089

noumenal is correct, you could do it with two loops. Another way though is to access the VECTOR using the original value though, writing that as 1, and setting all other values to zero.

To illustrate, first I make some fake data (with 4 original variables instead of 26) named X1 to X4.

*Fake Data.
SET SEED 10.
INPUT PROGRAM.
LOOP Id = 1 TO 20.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
VECTOR X(4,F2.0).
LOOP #i = 1 TO 4.
  COMPUTE X(#i) = TRUNC(RV.UNIFORM(1,62)).
END LOOP.
EXECUTE.

Now what this code does is create four vector sets to go along with each variable, then uses DO REPEAT to actually refer to the VECTOR stub. Then finishes up with RECODE - if it is missing it should be coded a 2.

VECTOR V1_ V2_ V3_ V4_ (61,F1.0).
DO REPEAT orig = X1 TO X4 /V = V1_ V2_ V3_ V4_.
  COMPUTE V(orig) = 1.
END REPEAT.
RECODE V1_1 TO V4_61 (SYSMIS = 2).

It is a little painful, as for the original VECTOR command you need to write out all of the stubs, but then you can copy-paste that into the DO REPEAT subcommand (or make a macro to do it for you).


For a more simple illustration, if we have our original variable, say A, that can take on integer values from 1 to 61, and we want to expand to our 61 dummy variables, we would then make a vector and then access the location in that vector.

VECTOR DummyVec(61,F1.0).
COMPUTE DummyVec(A) = 1.

For a record if A = 10, then here DummyVec10 will equal 1, and all the others DummyVec variables will still by system missing by default. No need to use DO IF for 61 values.

The rest of the code is just extra to do it in one swoop for multiple original variables.

Upvotes: 1

noumenal
noumenal

Reputation: 1237

You need a nested loop: two loops - one outer and one inner.

Upvotes: 0

Related Questions