Announcement

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

  • Generating new variable

    Hello,

    I have 5 variables named Home_1, Home_2, Home_3, Home_4 and Home_5. The answers are Yes or No. Now I need to create a new variable Home, which will show Yes if any of the 5 variables are Yes, otherwise the answer will be No. How can I do that?

    Fahmida

  • #2
    This is very close to an FAQ https://www.stata.com/support/faqs/d...ble-recording/

    The difference is that you want (if Yes is coded 1 and No is coded 2)

    Code:
    egen Home = rowmax(Home_*)
    or some variant on this (otherwise).

    (If your variables are all string, you should -- as you should any way -- give us an explicit data example: FAQ Advice #12.)
    Last edited by Nick Cox; 20 Aug 2018, 10:28.

    Comment


    • #3
      Hi there,

      Sorry I didn't give the examples for my previous dataset. Here is the explicit example-

      input str27(H44_1 H44_2 H44_3) str11 H44_4 byte H44_5
      "0. HAI (NO)" "0. HAI (NO)" "0. HAI (NO)" "" .
      "0. HAI (NO)" "0. HAI (NO)" "" "" .
      "0. HAI (NO)" "0. HAI (NO)" "" "" .
      "0. HAI (NO)" "0. HAI (NO)" "" "" .
      "0. HAI (NO)" "" "" "" .
      "0. HAI (NO)" "0. HAI (NO)" "" "" .
      "0. HAI (NO)" "0. HAI (NO)" "" "" .
      "" "" "" "" .
      "0. HAI (NO)" "" "" "" .
      "0. HAI (NO)" "0. HAI (NO)" "" "" .
      "" "" "" "" .
      "1. EE (YES)" "0. HAI (NO)" "" "" .
      "" "" "" "" .
      "0. HAI (NO)" "" "" "" .
      "0. HAI (NO)" "" "" "" .
      "0. HAI (NO)" "" "" "" .
      "" "" "" "" .
      "0. HAI (NO)" "0. HAI (NO)" "" "" .
      "0. HAI (NO)" "" "" "" .
      "" "" "" "" .
      end

      As you can see, I have 5 variables named H44_1, H44_2, H44_3, H44_4, and H44_5, and I need to create a new variable H44, which will contain "1. EE (YES)" if any of the 5 variables is "1. EE (YES)", and "0. HAI (NO)" if all the 5 variables are "0. HAI (NO)".

      Fahmida.

      Comment


      • #4
        So, in other words, neither your variable names nor their values were as reported in #1. Still, the principle is similar. I am going to recommend that you encode all those string variables. Then you should be where you want to be.

        Code:
        clear 
        
        input str27(H44_1 H44_2 H44_3) str11 H44_4 byte H44_5
        "0. HAI (NO)" "0. HAI (NO)" "0. HAI (NO)" "" .
        "0. HAI (NO)" "0. HAI (NO)" "" "" .
        "0. HAI (NO)" "0. HAI (NO)" "" "" .
        "0. HAI (NO)" "0. HAI (NO)" "" "" .
        "0. HAI (NO)" "" "" "" .
        "0. HAI (NO)" "0. HAI (NO)" "" "" .
        "0. HAI (NO)" "0. HAI (NO)" "" "" .
        "" "" "" "" .
        "0. HAI (NO)" "" "" "" .
        "0. HAI (NO)" "0. HAI (NO)" "" "" .
        "" "" "" "" .
        "1. EE (YES)" "0. HAI (NO)" "" "" .
        "" "" "" "" .
        "0. HAI (NO)" "" "" "" .
        "0. HAI (NO)" "" "" "" .
        "0. HAI (NO)" "" "" "" .
        "" "" "" "" .
        "0. HAI (NO)" "0. HAI (NO)" "" "" .
        "0. HAI (NO)" "" "" "" .
        "" "" "" "" .
        end
        
        label define H44 0 "0. HAI (NO)" 1 "1. EE (YES)", modify 
        
        forval j = 1/4 { 
            encode H44_`j', gen(HOME`j') label(H44) 
        }
        
        egen HOME = rowmax(HOME?) 
        
        list HOME*

        Comment


        • #5
          Thanks a lot! It actually worked, though I need to understand the principle more clearly. I'm trying to understand it, and thanks again for your suggestions and helps.

          Comment

          Working...
          X