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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With