Search code examples
vectorconcatenationspss

Vector definition with varying length


I'm trying to figure out how to write a vector command allowing for variable length.

Suppose that I have variables Item1 to Item7. I can write a vector statement...

VECTOR Item = Item1 to Item7.

In another situation I have 20 items instead of 7 (i.e., Item1 to Item20), and suppose I have a variable called num that contains the number 20.

How can I write a general purpose Vector statement that can perhaps use concat to combine "Item" and the value of num as a suffix?

Thanks!


Solution

  • This assumes that variable num has the same value in all rows.

    I'm first creating a little sample data to work with. For the example I will have the syntax create a vector with 13 variables.

    data list list/num item1 to item20 (21f5).
    begin data
    13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
    13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
    13 2 3 4 5 4 5 6 7 6 7 8 9 1 2 3 4 5 6 7 8
    end data.
    

    Now to create the vector, I first create the text of the command in a new variable:

    string VecCom (a50).
    compute VecCom=concat("VECTOR Item = Item1 to Item", ltrim(string(num,f5)), ".").
    

    Now I write it into a new syntax and run it:

    temp.
    select if $casenum=1.
    write out="somePath\create vector with specific length.sps" /VecCom.
    exe.
    insert file="somePath\create vector with specific length.sps".
    

    The vector is now defined - you can see it like this:

    display vectors.