Announcement

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

  • collapse with if condition

    Hi, I have the following collapse function:
    collapse (mean) mean_BARTAdj=BARTAdj (sd) sd_BARTAdj=BARTAdj (count) n=BARTAdj, by(weekday)
    which works just fine. Now, I would like to add a condition. I have another variable sense. If sense >=3 I would like to create a mean, sd and n and if sense <3, I would like to create mean, sd and n.
    Thank you very much!

  • #2
    You can use the -group()- function of egen to combine variables. In this way, you can get the breakdown that you want.

    Code:
    sysuse auto, clear
    gen goodcond= rep78>3
    lab def condition 0 "fair condition" 1 "good condition"
    lab values goodcond condition
    egen origin_condition= group(foreign goodcond), label
    collapse mpg weight turn, by(origin_condition)
    Res.:

    Code:
    . l
    
         +-------------------------------------------------------+
         |        origin_condition       mpg    weight      turn |
         |-------------------------------------------------------|
      1. | Domestic fair condition   19.1351   3,404.6    42.027 |
      2. | Domestic good condition   21.5333   3,101.3        40 |
      3. |  Foreign fair condition   23.3333     2,010   35.3333 |
      4. |  Foreign good condition        25   2,364.2   35.4211 |
         +-------------------------------------------------------+

    Comment

    Working...
    X