Announcement

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

  • Replace Not Working for Alpha-Numeric Observation Even After Destring

    Hi StataList,

    I am working on calculating exclusive breastfeeding rates for Nigeria, and have ran into some difficulties with the "replace" option. I receive an error message that says "type mismatch r(109)." When I tab my m4 variable because here are both alpha-numeric and numeric variables. I destrung the m4 variable to see if it would work and I still received an error message.

    Then, I tried to change my "exclusive breastfeeding" observation to a number, but this was not allowed.

    Below, I have provided the code I am using. The last line of code is where I am receiving the error message.

    gen water=0
    gen liquids=0
    gen milk=0
    gen solids=0
    gen breast=0

    * Water
    replace water=1 if (v470a>=1 & v470a<=7)

    * Other non-milk liquids
    * check for country specific liquids
    foreach xvar of varlist v470b v470c v470d v470i v470j v470k v470l* {
    replace liquids=1 if `xvar'>=1 & `xvar'<=7
    }

    * Powdered or tinned milk, formula, fresh milk
    foreach xvar of varlist v470e v470f v470g v470h {
    replace milk=1 if `xvar'>=1 & `xvar'<=7
    }

    * Solid food
    * check for country specific foods
    foreach xvar of varlist v470m v470n v470o v470p v470q v470r v470s v470t v470u v470v v470w v470x v470y v470z v470xx v470xy v470xz* {
    replace solids=1 if `xvar'>=1 & `xvar'<=7
    }

    * Still breastfeeding
    tab m4
    describe m4
    replace breast=1 if m4=="still breastfeeding"

    ================================================== =================================================
    The Stata Output after from *Still breastfeeding and below:


    . * Still breastfeeding
    . tab m4

    duration of |
    breastfeeding | Freq. Percent Cum.
    ---------------------+-----------------------------------
    1 | 1 0.05 0.05
    2 | 2 0.09 0.14
    5 | 2 0.09 0.23
    6 | 3 0.14 0.37
    7 | 2 0.09 0.46
    8 | 8 0.37 0.82
    9 | 6 0.27 1.10
    10 | 10 0.46 1.55
    11 | 8 0.37 1.92
    12 | 38 1.73 3.65
    13 | 11 0.50 4.15
    14 | 27 1.23 5.39
    15 | 26 1.19 6.57
    16 | 24 1.10 7.67
    17 | 25 1.14 8.81
    18 | 59 2.69 11.50
    19 | 24 1.10 12.60
    20 | 18 0.82 13.42
    21 | 8 0.37 13.78
    22 | 11 0.50 14.29
    24 | 6 0.27 14.56
    48 | 1 0.05 14.61
    never breastfed | 6 0.27 14.88
    still breastfeeding | 1,862 84.98 99.86
    dk | 3 0.14 100.00
    ---------------------+-----------------------------------
    Total | 2,191 100.00

    . describe m4

    storage display value
    variable name type format label variable label
    ----------------------------------------------------------------------------------------------------------------------------------------
    m4 byte %8.0g m4 duration of breastfeeding

    . replace breast=1 if m4=="still breastfeeding"
    type mismatch
    r(109);

    ================================================== =================================================

    Any suggestions as to why I cannot replace "still breastfeeding" within m4 as a numeric observation, or why my last replace command will not work would be greatly appreciated.

  • #2
    You've done quite some effort in explaining your problem, but in future posts, please use dataex for data examples. This provides all the details necessary for answering most questions. See the FAQ for more on how and why: https://www.statalist.org/forums/help#stata

    That said, your variable m4 is a byte (numeric), with labels applied. In your line with replace you should refer to the numeric value underlying the label "still breastfeeding". do:
    Code:
    tab m4, nolabel
    to figure out the correct values

    Comment

    Working...
    X