Announcement

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

  • converting string variables to numeric

    I have a string variable that I want to change to numeric but I can't. Can you help me please

    storage display value
    variable name type format label variable label
    ----------------------------------------------------------------------------------------------------------------
    d1 str18 %18s type de culture

    destring d1, replace
    d1 contains nonnumeric characters; no replace


    Attached Files

  • #2
    Your variable d1 is not a number. It is a list of numbers. So you need to break it up first. And you will end up with several variables (3 if the rest of your data looks like what you show--each containing a single number from the list).

    Code:
    split d1, gen(nd1) destring
    should do it.

    Comment


    • #3
      Your variable d1 seems to consist of between one and three separate numbers. Perhaps what you want are three new variables.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str6 d1
      "3 5 18"
      "3 18"  
      "3"     
      "3 5 18"
      ""      
      end
      split d1, generate(d1_) destring
      describe
      list
      Code:
      . split d1, generate(d1_) destring
      variables born as string: 
      d1_1  d1_2  d1_3
      d1_1: all characters numeric; replaced as byte
      (1 missing value generated)
      d1_2: all characters numeric; replaced as byte
      (2 missing values generated)
      d1_3: all characters numeric; replaced as byte
      (3 missing values generated)
      
      . describe
      
      Contains data
       Observations:             5                  
          Variables:             4                  
      ------------------------------------------------------------------------------------------------
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      ------------------------------------------------------------------------------------------------
      d1              str6    %9s                   
      d1_1            byte    %10.0g                
      d1_2            byte    %10.0g                
      d1_3            byte    %10.0g                
      ------------------------------------------------------------------------------------------------
      Sorted by: 
           Note: Dataset has changed since last saved.
      
      . list
      
           +-----------------------------+
           |     d1   d1_1   d1_2   d1_3 |
           |-----------------------------|
        1. | 3 5 18      3      5     18 |
        2. |   3 18      3     18      . |
        3. |      3      3      .      . |
        4. | 3 5 18      3      5     18 |
        5. |             .      .      . |
           +-----------------------------+
      For future reference, please note the use of dataex to present example data that can then be copied and pasted into Stata code for testing and demonstration. Screenshots are much less useful than one might imagine.

      Comment


      • #4
        I did it and it worked.
        thank you so much Clyde

        Comment


        • #5
          thank you so much William

          Comment

          Working...
          X