title: Ignore missing values when generating new variable
body: <p>I want to create a new variable in Stata, that is a function of <code>3</code> different variables, <code>X</code>, <code>Y</code> and <code>Z</code>, like:</p>
  
  <pre><code>gen new_var = (((X)*3) + ((Y)*2) + ((Z)*4))/7
  </code></pre>
  
  <p>All observations have missing values for one or two of the variables. </p>
  
  <p>When I run the aforementioned command, all it generates are missing values, because no observation has values for all <code>3</code> of the variables. I would like Stata to complete the function ignoring the missing variables.</p>
  
  <p>I tried the following commands without success:</p>
  
  <pre><code>gen new_var= (cond(missing(X*3),., X) + cond(missing(Y*2),., Y))/7 
  gen new_var= (!missing(X*3+Y*2+Z*4)/7)
  gen new_var=  (max(X , Y, Z)/7) if missing(X , Y, Z) 
  </code></pre>
  
  <p>The <code>egen</code> command does not allow complicated functions; otherwise <code>rowtotal()</code> could work.</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>To clarify, "ignoring missing variables" means that even if any one of the component variables is not missing, then apply the function to only that variable and produce a value for the new variable. The new variable should have missing values only when all three component variables are missing. </p>