RTrain3k
RTrain3k

Reputation: 867

Create indicator variables in SPSS using loops

I am trying to create some indicator variables in SPSS using a loop.

I want to create 17 new variables ABA(1-17) that take a value of 1 if BA(1-17) equals 1. I was trying something like:

VECTOR ABA(17).
LOOP #i = 1 to 17.
IF(BA(#i)=1) ABA(#i) = 1.
END LOOP.
EXECUTE.

This, unfortunately, just creates variables with missing values. Does the above code just need a small tweak or is their a more efficient way to accomplish creating the variables?

Upvotes: 3

Views: 405

Answers (1)

Jignesh Sutar
Jignesh Sutar

Reputation: 2929

I believe you need to define the set of BA variables as a vectors before you can reference them as such in the code. So try:

VECTOR ABA(17) /BA=BA1 to BA17.
LOOP #i = 1 to 17.
IF(BA(#i)=1) ABA(#i) = 1.
END LOOP.
EXECUTE.

Note, given the BA variables already exist in the dataset you cannot reference them as VECTOR BA(17). but instead must use VECTOR BA=BA1 to BA17. If they are not in order in the datafile then you will have to get them in order using ADD FILES FILE to re-arrange variable ordering.

Upvotes: 1

Related Questions