3

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?

RTrain3k
  • 845
  • 1
  • 13
  • 27

1 Answers1

1

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.

Jignesh Sutar
  • 2,909
  • 10
  • 13
  • You are correct! I actually did try 'VECTOR BA(17)' and it did not work. – RTrain3k Nov 10 '16 at 17:34
  • I don't know why `SPSS` doesn't allow just that but it bugs me also, especially so when variables are not in order. If they are not, I tend to jump to python for a solution...which is much better at handling these types of dynamics. – Jignesh Sutar Nov 10 '16 at 17:39
  • @JinneshSutar do you know of any good online resources that explain how to create loops and vectors in SPSS? – RTrain3k Nov 10 '16 at 17:39
  • The Programming and Data Management book is great resource: https://developer.ibm.com/predictiveanalytics/wp-content/uploads/sites/48/2015/04/Programming-and-Data-Management-for-IBM-SPSS-Statistics-23.pdf – Jignesh Sutar Nov 10 '16 at 17:46
  • 2
    You actually don't need to loop here. `RECODE ABA1 TO ABA17 (1 = 1)(ELSE = SYSMIS) INTO BA1 TO BA17.` would produce equivalent results. – Andy W Nov 10 '16 at 18:27
  • @AndyW agreed! Would opt for that approach myself also. Likely much more efficient too and syntax much easier to read. – Jignesh Sutar Nov 10 '16 at 19:11
  • 1
    As a general matter, when you need to use a long list of noncontiguous variables, the STATS SELECT VARIABLES (Utilities > Define Variable Macro) extension command can often help. It defines a macro listing variables selected by metadata such as a pattern in the names. In this case, it might be something like SPSSINC SELECT VARIABLES MACRONAME="!BA" /PROPERTIES PATTERN = "BA". That macro could be used anywhere that a variablel list is acacepted. Other properties such as type can also be specified. – JKP Nov 11 '16 at 14:40