Announcement

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

  • numeric and string variables: replace a single value of a numeric variable in a specific index

    Dear Stata users,

    I would like to ask your help. I have a numeric variable named "nmf" and a string variable named "nmf_str".

    I want to replace the value of the numeric variable at index 5 with the value of the string variable at the same index.
    The numeric variable contains names of funds that I previously converted to a numeric format.
    The string variable only contains a value at index 5.

    My code is the following:

    . local new_value = real(nmf_str[5])

    . replace nmf = `new_value' in 5
    (1 real change made, 1 to missing)


    In the data editor the previous value of the numeric variable at index 5 is now a missing value.

    I very much appreciate any help!

    Best,

    Chwen Chwen



  • #2
    Hard to know what is happening but the use of a local macro is not needed here.

    Think of these steps.

    I have a pen. I put it in a box. I take it out of the box. Now I have a pen again.

    to which a natural question is Why did you fool around with the box there?

    In short,

    Code:
    replace nmf = real(nmf_str) in 5
    is the way to do that directly.

    Comment


    • #3
      Thank you Nick!

      I think that I got lost and stopped using my brain for a while...

      I got the message below after running the code line:


      . replace nmf=real(nmf_str) in 5
      (1 real change made, 1 to missing)

      which is the same message I got previously.

      I report what the data editor shows in the table below (I had to change the names for confidentiality issues):

      nmf_str nmf
      4 m1
      5 s1 .

      As one can see, the variable nmf displays a missing value instead of "s1".


      I also checked both variables but cannot find anything unusual:


      . descr nmf_str nmf

      Variable Storage Display Value
      name type format label Variable label
      --------------------------------------------------------------------------
      nmf_str str20 %20s
      nmf long %101.0g nmf fund name

      .

      Why does this happen?
      Any suggestion about how I can replace the value at index 5?

      Thank you.

      Chwen Chwen

      Comment


      • #4
        Indeed. Stata can't see that the string sm1 really is a number wrapped in string form.

        Code:
        di real("sm1")
        returns missing. What did you expect to happen there and why?

        Comment


        • #5
          In #4 the illustration should be in terms of s1 not sm1 but the principle is the same.

          Comment


          • #6
            I have noticed your answer right now, thank you Nick.



            Comment

            Working...
            X