Tuesday, February 07, 2012

Tips - Stata: function and extended function to -generate- and -egen-

Tips - Stata: function vs. extended function & -generate- vs. -egen-


Stata provids two kinds of functions: regular functions and extended functions (or called egen function). These functions need to be used with other Stata commands, usually -generate- and -egen-. -generate- is for regular function, or -egen- for egen function and only -egen- may be used to run egen function. some function may look similar but different:


  • sum(var) and total(var) (a -egen- function)with -bysort-
    • Sum() is a regular function. When you use .gen sum1=sum(var), it will generate a new variable named 'sum1' with cumulative summation of 'var' from top to bottom in order of group.
    • However, total() as a egen function used in .egen sum2=total(var), it will generate a new variable named 'sum2' with summation of 'var' of all observation from top to bottom in that group. All the cases in that group have the same values of 'sum2'.
  • recode(var,cutpt1,cutpt2,cutpt3,...),  irecode(var,x1,x2,...,xn) and group(varlist) (a -egen- function)



No comments: