Announcement

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

  • Recode multiple observations to missing

    Hello Statalist

    I need help on how i can recode multiple observations to missing. In the data example below welfare is a clone of var5, but i only want observations with certain values to be cloned. Also, i want the values in welfare to be set to missing in var5.

    I am aware of the recode var5 (1/2=.), generate(welfare) command, however, this command only seems to work with two values. Thus, i cannot write recode var5 (1/2/3=.) as stata tells me "unknown el / in rule r(198);". It also doesnt set the values in welfare to missing in var5.


    Any help is much appreciated!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(var5 welfare)
     6  6
     9  9
    10 10
    24 24
    23 23
     4  4
    18 18
    15 15
    18 18
     7  7
    18 18
     7  7
     6  6
     4  4
    10 10
    11 11
     2  2
    23 23
     5  5
     5  5
    end
    Last edited by Carlos Ingeberg; 10 May 2022, 16:11.

  • #2
    Sorry, but what do you mean by "certain values"?

    My guess is that you want to replace not recode here.

    Comment


    • #3
      Hi Nick, thanks for a quick reply!

      By certain values i mean that i want observations with values 5, 7, 14, 15, 38, 40 and 42 to be cloned to welfare and set to missing in var5.

      Comment


      • #4
        Sorry, I am still guessing. here. Is this what you want? Or show enough technique?

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte var5
         6
         9
        10
        24
        23
         4
        18
        15
        18
         7
        18
         7
         6
         4
        10
        11
         2
        23
         5
         5
        end
        . 
        . clonevar welfare = var5 if inlist(var5, 5, 7, 14, 15, 38, 40, 42)
        . 
        . list 
        
             +----------------+
             | var5   welfare |
             |----------------|
          1. |    6         . |
          2. |    9         . |
          3. |   10         . |
          4. |   24         . |
          5. |   23         . |
             |----------------|
          6. |    4         . |
          7. |   18         . |
          8. |   15        15 |
          9. |   18         . |
         10. |    7         7 |
             |----------------|
         11. |   18         . |
         12. |    7         7 |
         13. |    6         . |
         14. |    4         . |
         15. |   10         . |
             |----------------|
         16. |   11         . |
         17. |    2         . |
         18. |   23         . |
         19. |    5         5 |
         20. |    5         5 |
             +----------------+
        
        . 
        . replace var5 = . if !missing(welfare)

        Comment


        • #5
          This is exactly what i was looking for. Thank you so much!

          Comment

          Working...
          X