Announcement

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

  • How to generate new variables using -gen- while ignoring missing cases?

    Hello folks, I need to create a new variable based on the calculation of two existing variables (A and B) but in the meantime, there are missing cases in both A and B. How could I get A/B while letting Stata ignore all the missing cases? I'm aware that -egen- can work with missing option but not -gen-. I don't want to treat missing as 0 as well. Any ideas please? Thanks!

  • #2
    I'm not sure that I understand what you're after. generate doesn't treat missing as zero. You're not looking for something like this, are you?
    Code:
    summarize A, meanonly
    tempname A
    scalar define `A' = r(mean)
    summarize B, meaonly
    generate double AoverB = `A' / r(mean)

    Comment


    • #3
      Like Joseph, I'm not sure I understand; however,
      Code:
      gen newvar = A/B
      will give you the ratio when there are no missing values in an observation and will be missing if either A or B (or both) are missing

      Comment

      Working...
      X