Announcement

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

  • egen for numeric variables

    Hi, I'm trying to concat two numeric variables whit the egen command, I know when it is used a string variable results, but, when I try to destring a "non-numeric characters" appears and the variable isn't replace. I need to do math operations with this variable so I can't leave it as a string. If you know the solution, I would be very grateful for your help.


    e.g. I got two weight variables p1005peso_2 for an integer number and p1005peso_2 for a fraction of the weight
    +----------+ +----------+
    | p1005p~2 | | p100~o_1 |
    |----------| |----------|
    1. | 103 | 1. | 6 |
    2. | 54 | 2. | 9 |
    3. | 62 | 3. | 8 |
    4. | 61 | 4. | 5 |
    5. | 70 | 5. | 4 |
    +----------+ +----------+

    **
    egen peso_con = concat(p1005peso_2 p1005peso_1), punct(.)

    . list peso_con in 1/5

    +----------+ this is how I need the variable shown, but numeric
    | peso_con |
    |----------|
    1. | 103.6 |
    2. | 54.9 |
    3. | 62.8 |
    4. | 61.5 |
    5. | 70.4 |
    +----------+

    in the missing values shows ". . ."

    ** destring peso_con, replace
    peso_con: contains nonnumeric characters; no replace

  • #2
    I feel guilty here for first writing egen, concat() and destring which seem like complete distractions for what I guess is your real problem.

    Isn't this just

    Code:
    gen wanted = p1005peso_2  + p1005peso_1/10
    ?

    PS I can't see any non-numeric characters in your example, but presumably they occur in other observations.

    Comment


    • #3
      Nick, thanks for the answer is just what i need it, I'm new in this stata world, so honestly I don't understand why the command egen doesn't work in the way I tried to use it, I would appreciate it if you could give me an explanation or refer me to a space to read it.

      list peso_con in 9/15 this are the non-numeric characters

      +----------+
      | peso_con |
      |----------|
      9. | 57.0 |
      10. | ... |
      11. | 50.7 |
      12. | 82.0 |
      13. | 54.9 |
      |----------|
      14. | ..4 |
      15. | ..5 |
      +----------+

      Comment


      • #4
        It works like this. You were there trying to concatenate missing (.) with missing or a digit with punctuation . (decimal point). But Stata has no numeric interpretation of ... ..4 or ..5 -- which is what is needed for destring to work.

        Comment


        • #5
          thank you.

          Comment

          Working...
          X