Announcement

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

  • Using bysort egen

    Hi everyone,

    I have two questions:

    1/ I am using the command
    Code:
    bysort $id: egen
    . The result I got containing the missing value. That is because the variable used for the [egen] has missing value. However, I would like to get the result that the missing values replace by zero value. So how can I get the result whereas the missing value replaced by 0 value when using the command
    Code:
    bysort $id: egen
    2/ What is the correct for percentile command to calculate the variables: say average income of 20% of the lowest income people, 20% of the lower-middle income people, 20% of the middle income people; 20% of high-middle income people and 20% of highest income people?

    Thank you a lot
    Regards
    Linh
    Last edited by Linh mt; 03 Nov 2019, 05:02.

  • #2
    Linh:
    as you can see from the following toy-example, your code won't get you any far:
    Code:
    . use "C:\Program Files (x86)\Stata15\ado\base\a\auto.dta"
    (1978 Automobile Data)
    
    . bysort foreign: egen
    invalid syntax
    r(198);
    
    .
    Hence, please report exactly what you typed and what Stata gave you back (as per FAQ). Thanks.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Linh:
      as you can see from the following toy-example, your code won't get you any far:
      Code:
      . use "C:\Program Files (x86)\Stata15\ado\base\a\auto.dta"
      (1978 Automobile Data)
      
      . bysort foreign: egen
      invalid syntax
      r(198);
      
      .
      Hence, please report exactly what you typed and what Stata gave you back (as per FAQ). Thanks.
      Hi Carlo,

      Thank you for your reply. Actually, my code is
      Code:
      bysort $id: egen cap1=count(GDPT) if GDPT==1
      bysort $id: egen cap2=count(GDPT) if GDPT==2
      bysort $id: egen cap3=count(GDPT) if GDPT==3
      . The result I got is that:
      cap1 cap2 cap3
      . 1 1
      . . .
      1 1 .
      1 , .
      . , 1
      My question is I would like to replace the missing value by 0 value.

      Regarding my second question. I tried to use the code
      Code:
      summarize incomehousehold, detail
      but the result is not 20% of the lowest income people. So if I want to get the result as I wish (the question in #1), what command should I use?

      Thank you again
      Kind regards
      Linh
      Last edited by Linh mt; 03 Nov 2019, 08:58.

      Comment


      • #4
        Linh:
        David Benson already gave good advice about your second question at https://www.statalist.org/forums/for...come-household.
        As far as your first question is concerned, you may want to try something along the following lines:
        Code:
        . set obs 3
        number of observations (_N) was 0, now 3
        
        . g var1=1 in 1/2
        (1 missing value generated)
        
        . g var2=1 in 2/3
        (1 missing value generated)
        
        . g var3=1
        
        . replace var3=. in 2
        (1 real change made, 1 to missing)
        
        . foreach var of varlist var1-var3 {
          2. replace `var'=0 if `var'==.
          3.  }
        (1 real change made)
        (1 real change made)
        (1 real change made)
        
        . list
        
             +--------------------+
             | var1   var2   var3 |
             |--------------------|
          1. |    1      0      1 |
          2. |    1      1      0 |
          3. |    0      1      1 |
             +--------------------+
        
        .
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Thank you @Carlo Lazzaro

          Comment

          Working...
          X