categorize string variable

Discussion in 'SPSS' started by Dag, Nov 9, 2011.

  1. Dag

    Dag Guest

    Hello All,
    In our health care sample we have one string variable with values as
    shown below. The values are combinations of diagnoses separated by
    commas. We want to categorize according to example below, where eg "9"
    of course means "a combination of nine diagnoses". I suppose this
    could be done according to the number of commas in each string?
    Grateful for any suggestions about suitable code for this.
    Dag, Stockholm, Sweden

    VARIABLE CATEGORIZED
    VARIABLE
    0
    lf1 1
    0
    lf1,lf11 2
    lf1,lf2 2
    lf1,lf2,lf4,lf6,lf7,lf8,lf9,lf10,lf16 9
    lf1,lf7 2
    lf1,lf7,lf16 3
    lf10,lf14,lf15,lf16,lf17 5
     
    Dag, Nov 9, 2011
    #1
    1. Advertisements

  2. Dag

    David Guest

    data list / strfield 1-40 (A).
    begin data
    0
    lf1 1
    0
    lf1,lf11 2
    lf1,lf2 2
    lf1,lf2,lf4,lf6,lf7,lf8,lf9,lf10,lf16 9
    lf1,lf7 2
    lf1,lf7,lf16 3
    lf10,lf14,lf15,lf16,lf17 5
    end data.

    STRING #STR (A40).
    COMPUTE #Str=strfield.
    DO IF LEN(LTRIM(#STR)) > 0.
    + COMPUTE NDIAG=1.
    + LOOP.
    + COMPUTE #=INDEX(#STR,",").
    + DO IF # > 0.
    + COMPUTE NDIAG=NDIAG+1.
    + COMPUTE #STR=SUBSTR(#STR,#+1).
    + END IF.
    + END LOOP.
    ELSE.
    + COMPUTE NDIAG=0.
    END IF.

    LIST.
     
    David, Nov 9, 2011
    #2
    1. Advertisements

  3. Dag

    Dag Guest

    Thank you David, exactly what we need, very helpful!
     
    Dag, Nov 11, 2011
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.