Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Destringing values

    I have a column which is a mixture of numbers and words eg:

    Less than 10
    Null
    More than 30

    The above are stored as string values

    TRY 1: - I understand this is used for numerical data as it won't work with encode
    In order to destring them i have tried:

    gen numerical = real (stringvalue)

    the column came up with just hyphens - no blue text

    then i tried:

    TRY 2: - I understand encode should be used for just qualitative data
    encode stringvalue, gen (numerical)

    Some of them were successfully encoded, some weren't eg Less than 10 was successfully encoded
    And I only managed to replace this value with a number 1 the rest I coudln't as they weren't encoded

    What am I doing wrong?

  • #2
    Hello Martin. What do you want each of those example strings to look like in the resulting numeric variable? Thanks for clarifying.

    Less than 10 = ?
    Null = ?
    More than 30 = ?

    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 18.5 (Windows)

    Comment


    • #3
      Hi Bruce, thanks for replying
      I want:

      1 = Less than 10
      0 = Null
      3 = More than 30


      Hence I want to destring the values
      Then replace with numbers

      Comment


      • #4
        Would you settle for whatever numeric values -encode- assigns? The following works for me using the 3 sample string values you gave:

        Code:
        Code:
        clear
        input str15 svar
        "Less than 10"
        "Null"
        "More than 30"
        end
        
        encode svar, generate(nvar)
        list
        list, nolabel
        Output:
        Code:
        . clear
        
        . input str15 svar
        
                        svar
          1. "Less than 10"
          2. "Null"
          3. "More than 30"
          4. end
        
        .
        . encode svar, generate(nvar)
        
        . list
        
             +-----------------------------+
             |         svar           nvar |
             |-----------------------------|
          1. | Less than 10   Less than 10 |
          2. |         Null           Null |
          3. | More than 30   More than 30 |
             +-----------------------------+
        
        . list, nolabel
        
             +---------------------+
             |         svar   nvar |
             |---------------------|
          1. | Less than 10      1 |
          2. |         Null      3 |
          3. | More than 30      2 |
             +---------------------+
        
        .

        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment

        Working...
        X