Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 305
RTrain3k Avatar asked May 12 '26 17:05

RTrain3k


1 Answers

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.

like image 61
Jignesh Sutar Avatar answered May 15 '26 12:05

Jignesh Sutar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!