Announcement

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

  • Convert a string variable to a numeric (double) variable

    Hello

    I am trying to convert a string variable in one year (2013) of my data, so that I can append it to the remaining years (2009-2012) in my dataset.

    The variable is simply an ID number that corresponds to certain hospitals in a given state, so there are no hypens or other characters to consider.

    So far I have tried the following:

    destring AHAID, generate(AHAID_n) - which converts it to numeric (long)

    gen AHAID_n = real(AHAID) - which converts it to numeric (float).

    Neither of these options allow me to append this year with the previous years where that variable is numeric (double), and when I use the force command it just treats it as missing.

    please help. Thanks

  • #2
    What you describe as happening does not make sense to me, and I cannot reproduce the phenomenon in my Stata.

    If you successfully -destring- AHAID and it becomes numeric (or float) and then append to a data set that has it as a double, the append should work. I have never seen it fail, and here's an example of it working:

    Code:
    clear*
    sysuse auto
    gen double ratio = price/mpg
    tempfile data1
    save `data1'
    
    sysuse auto, clear
    gen ratio = price/mpg
    append using `data1'
    Here's what I think is going wrong. In your 2009-2012 data set you have a variable AHAID which is a double. The code you show in #1 generates a new and different variable AHAID_n which converts that into a number. Which is all very nice, but when you append, Stata still thinks you are trying to append the string AHAID from the 2013 data set to the double AHAID data set. It does not read your mind to know that you mean for it to use AHAID_n from the 2013 data set.

    So what you need to do, if I have guessed correctly, is, in your 2013 data set, to -drop AHAID- and then -rename AHAID_n AHAID-. After that, the -append- should go smoothly.


    Comment

    Working...
    X