Announcement

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

  • unknown function () in a simple generate command

    Hi,

    I am trying to generate a new variable b with the following formula using all existing variables,

    Code:
    gen b = .25*((((mean_dr0-mean_dr)(mean_or0-mean_or))/var_or) + (r_ordr0*(var_or0/var_or)))  ///
          + .25*((((mean_dr1-mean_dr)(mean_or1-mean_or))/var_or) + (r_ordr1*(var_or1/var_or)))   ///
          + .25*((((mean_dr2-mean_dr)(mean_or2-mean_or))/var_or) + (r_ordr2*(var_or2/var_or)))   ///
          + .25*((((mean_dr3-mean_dr)(mean_or3-mean_or))/var_or) + (r_ordr3*(var_or3/var_or)))
    and I get back

    unknown function ()
    I just don't see what's wrong with the equation. Any thoughts?

    I did not copy the text from a text editor. I am using Stata 16.
    Thanks
    Herman
    Last edited by Herman vandeWerfhorst; 21 Sep 2022, 01:51.

  • #2
    The parser baulks at expressions like

    Code:
    (mean_dr0-mean_dr)(mean_or0-mean_or)
    Stata does not follow the convention in mathematics that adjacent terms are multiplied together.

    So you need an operator there, perhaps * or /.

    I will assume for my convenience that the operator should be * (forgive me, but I don't recognise the formula has having a meaning standard in my field). The point to be made next applies to any other operator too.

    I would often build up an expression like that in steps:

    Code:
    local call 0  
    
    forval j = 0/3 {          
        local call `call' +  (((mean_dr`j'-mean_dr) * (mean_or`j'-mean_or))/var_or) + (r_ordr`j'*(var_or`j'/var_or))
    }
    
    gen b  = (`call') / 4
    Here the initial 0 is not needed in your code, but it does help in mine. It is crucial in this code not to use the = sign in defining the macro.

    An equivalent would be to define the expression

    Code:
     
    
    gen b = 0 
    
    forval j = 0/3 { 
          replace b = b + (((mean_dr`j'-mean_dr) * (mean_or`j'-mean_or))/var_or) + (r_ordr`j'*(var_or`j'/var_or))
    } 
    
    replace b = b  / 4
    Dividing once by 4 is surely preferable to multiplying 4 times by 0.25 and makes the point that you are taking an average.
    Last edited by Nick Cox; 21 Sep 2022, 02:42.

    Comment


    • #3
      thanks!

      Comment

      Working...
      X