Announcement

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

  • how to replace string value into numerical value?

    Hi, I am working with data about birth history and have a variable 'age at death in years. The values under this variable are all numerical except for some of them which are "< 1 month". I would like to change this into a number but I'm struggling with how to go about doing that. I don't want to create a whole new variable - I just want to replace the "< 1 month" with 0. Does anyone know how to help me with this?

    Thank you!

  • #2
    to replace "< 1 month" with "0" and convert to numeric,
    Code:
    replace variable = "0" if variable == "< 1 month"
    destring(variable), replace

    Comment


    • #3
      Code:
      replace age_at_death = "0" if age_at_death == "< 1 month"
      destring age_at_death, replace
      will give you a true numeric variable (not just one that looks numeric to human eyes but Stata will not be able to calculate with).

      Comment


      • #4
        if I understand you correctly, try the following (but please show a -dataex- example in future as discussed in the FAQ):
        Code:
        replace var="0" if var=="< 1 month"
        desiring var, replace
        obviously you should replace "var" in the above with the actual variable name as it exists in your data set

        Comment


        • #5
          Hi everyone, thank you so much for your reply! Unfortunately, this doesn't seem to work? when I do

          replace var = "0" if var == "<1 month" it says there is a type mismatch and then doing destring var, replace, it says the variable is already numberic, which is strange because it clearly has the symbol < and word month!

          I don't use Stata that often so I'm really not sure where I am going wrong

          Comment


          • #6
            This means that the variable is a numeric variable with labeled values. To display numeric codes rather than value labels type -tabulate var, nolabel-.
            You can display the name of the value label by typing -describe var- and remove it by typing -label drop value_label_name-.
            Also check out -help label-.

            Comment


            • #7
              I will just take the opportunity here to point out again, for whoever is reading along, how asking for help with code without providing adequate (or any, in this case) example data just wastes everybody's time. Had O.P. used the -dataex- command to show example data in #1, he or she would have had a correct solution within less than one hour. Instead, 3 people took the time to respond to the inquiry, all giving identical, but ultimately useless advice, and O.P. then wastes his or her own time trying to implement the incorrect solution, and has to then post again to get help.

              It can't be said often enough, or strongly enough: When asking for help with code, always show example data, and always use -dataex- to do that.

              Comment

              Working...
              X