SPSS Statistics

 View Only
  • 1.  Loops & Vectors

    Posted Thu September 22, 2022 02:35 PM
    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
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Loops & Vectors

    Posted Thu September 22, 2022 02:53 PM
    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 Thu September 22, 2022 03:00 PM
    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 Thu September 22, 2022 03:06 PM
    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 Thu September 22, 2022 03:08 PM
    there's only one valid number per row.

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



  • 6.  RE: Loops & Vectors

    Posted Thu September 22, 2022 03:32 PM
    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 Thu September 22, 2022 03:40 PM
    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 Thu September 22, 2022 04:01 PM
    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 Fri September 23, 2022 08:44 AM
    Thanks Rick appreciate it. - Arthur

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