SPSS Statistics

 View Only
  • 1.  Loops & Vectors

    Posted 11 days ago
    Afternoon,

    I need to go through multiple columns and identify which variables start with a 3 4 or 5 and then copy that whole variable into a new one.  I also need to run loops through these to replace ',' with a ''.   I was thinking something like this below.  


    vector file=V0 to V144.
    loop i=1 to 14.
    if char.substr(i, char.index(i, '3', '4', '5')) file=i.'
    compute i=replace(',','').
    end loop.
    execute.

    ------------------------------
    Art Jack
    ------------------------------


  • 2.  RE: Loops & Vectors

    Posted 11 days ago
    Sorry, but like usual I'm unclear about exactly what you want to see.

    "variables start with a 3 4 or 5 and then copy that whole variable into a new one."

    Copy the value of each variable that begins with any(3,4,5) into a new (numeric) variable that contains that value? So, for each case, there could possibly be 145 new numeric variables (if each of V0 to v144 begin with any(3,4,5))?

    Can you please provide me an example of what you expect to see when whatever code is used runs as you want it to?

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 3.  RE: Loops & Vectors

    Posted 11 days ago
    Hi Rick, Anytime v1 to v3 starts with a 3,4, or 5 I want to create a variable that equals the whole cell of v1 to v3.  But I don't want to copy fields that don't start with those numbers.  But I need to this for V0 to V144. - thanks. 



    v1 v2 v3 new_variable
    323521 323521
    336258 336258
    452521 452521
    515485 515485
    Bob
    95852
    555548 555548
    AC222585


    ------------------------------
    Art Jack
    ------------------------------



  • 4.  RE: Loops & Vectors

    Posted 11 days ago
    OK, that helps. But what if V1 and V3 both begin with 3,4 or 5? That means two new variables, with the values of V1 and V3, right?

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 5.  RE: Loops & Vectors

    Posted 11 days ago
    there's only one valid number per row.

    ------------------------------
    Art Jack
    ------------------------------



  • 6.  RE: Loops & Vectors

    Posted 11 days ago
    OK, that's very helpful. I'll assume also that V1 to V3 are numeric in the data. If they are defined as strings, please tell me.

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 7.  RE: Loops & Vectors

    Posted 11 days ago
    So, here's the first part:

    data list list /v1 to v3 (3A8).
    begin data.
    323521 . .
    . 336258 .
    452521 . .
    . . 515485
    Bob . .
    95852 . .
    555548 . .
    . AC222585 .
    end data.

    vector v=v1 to v3.
    string newvar (A8).
    loop #i=1 to 3.
    if any(char.substr(v(#i),1,1),'3','4','5') newvar=v(#i).
    end loop.
    execute.
    list.

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 8.  RE: Loops & Vectors

    Posted 11 days ago
    And here is both the first part and the second (removing commas):

    data list list /v1 to v3 (3A8).
    begin data.
    323521 . .
    . 336258 .
    452521 . .
    . . 515485
    Bob . .
    95852 . .
    555548 . .
    . AC222585 .
    "13,456" . .
    end data.

    vector v=v1 to v3.

    string newvar (A8).

    loop #i=1 to 3.
    * in any v(#i), replace ',' with a ''. .
    compute v(#i)=replace(v(#i),",",'').
    if any(char.substr(v(#i),1,1),'3','4','5') newvar=v(#i).
    end loop.
    execute.
    list.

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 9.  RE: Loops & Vectors

    Posted 10 days ago
    Thanks Rick appreciate it. - Arthur

    ------------------------------
    Art Jack
    ------------------------------