Announcement

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

  • Merging multiple binary variables and hereby ignoring missing values

    Dear all

    I have seven binary variables that I want to merge into one binary variable.
    My seven variables (Rejected1, Rejected2, ..., Rejected7) all have value 1 if the firm is rejected for a credit, 0 otherwise.
    Now I want to make RejectedMerge, the problem is that all the missing values of the seven variables are always included as 1 in RejectedMerge...
    How do I make sure that they are excluded from the values 0 and 1 and thus that they just remain missing values?

    I have already tried:
    RejectedMerge = Rejected1 & Rejected2 &... & Rejected7 (also tried it with *)
    egen RejectedMerge = group (Rejected1 Rejected2 ... Rejected7) (aslo used rowtotal)
    but all of them include missing values

    Thank you very much in advance!
    Last edited by Elise Sobrie; 12 Jan 2018, 07:43.

  • #2
    Elise: your question is confusing. If you used dataex (as you are told in FAQ #12), things could have probably been easy to understand.
    Having said that,
    Is this what you are aiming for?

    Code:
    clear
    set obs 3
    input byte(rej1 rej2 rej3 rej4)
    1 0 1 .
    0 1 0 0
    0 0 1 1
    egen rej_tot = rowtotal(rej1 rej2 rej3 rej4)
    Note: In the example, there are only 4 variables; but this can be extended to 7. rej_tot is not binary; but shows a total number of times a firm is rejected (2 for 1 & 3).
    Last edited by Abdul Adam; 12 Jan 2018, 07:57.

    Comment


    • #3
      Elise:
      another approach might be:
      Code:
      . set obs 10
      number of observations (_N) was 0, now 10
      
      . g A=1 in 1/9
      (1 missing value generated)
      
      . g B=1 in 1/7
      (3 missing values generated)
      
      . foreach var of varlist A-B {
        2.
      .                 replace `var'=999 if `var'==.
        3.
      .         }
      (1 real change made)
      (3 real changes made)
      
      . egen count=rowtotal(A B)
      
      . replace count=count-999 if count>999
      (3 real changes made)
      
      . foreach var of varlist A-B {
        2.                 sum `var' if `var'!=999
        3.         }
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
                 A |          9           1           0          1          1
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
                 B |          7           1           0          1          1
      However, please note that, -egen- with -rowtotal- function returns missing values as zeros:
      Code:
      . set obs 10
      number of observations (_N) was 0, now 10
      
      .
      . g A=1 in 1/9
      (1 missing value generated)
      
      .
      . g B=1 in 1/7
      (3 missing values generated)
      
      .
      . egen count=rowtotal(A B)
      
      . tab count
      
            count |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                0 |          1       10.00       10.00
                1 |          2       20.00       30.00
                2 |          7       70.00      100.00
      ------------+-----------------------------------
            Total |         10      100.00
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Hallo

        I guess I explained it somewhat ambiguous (I just find it hard to explain) because I don't want to count the frequencies..
        I added a picture of a sketch of what I want to obtain. It is a kind of a list under the variable name Rm (RejectedMerged), so that each firm has now 7 observations related to the 7 types of credits. I don't know that what I want is actually possible in Stata...
        If it is still not clear, please ask me
        Attached Files

        Comment


        • #5
          Both, thank you very much for your effort!

          Carlo, when I try your code I get an error:
          . foreach var of varlist Rejected1 Rejected2 Rejected3 Rejected4 Rejected5 Rejected6 Rejected7 {
          2. replace 'var' = 999 if 'var' ==.
          3. }
          ' invalid name
          r(198);

          What is going wrong?

          Comment


          • #6
            Elise:
            try again replacing
            Code:
            'var'
            with
            Code:
            `var'
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              I want to create a categorical variable with binary variables which takes 1 if true and 0 otherwise so I used the syntax below.

              egen byte new = rowtotal(childfever2 childcough5 underweight)

              If I get 0,1,2 and 3 , what do they mean?

              Comment


              • #8
                The number you get after rowtotal() is the number of 1's for a particular respondent on that particular observation. What that means substantively depends on the binary variables and what they mean. For example, each question could be part of a scale or index, and a greater value on the new variable might indicate a higher level of the underlying latent construct you want to measure. It depends on what your variables represent.

                Comment

                Working...
                X