*! gdgen2    0.2.1 2008-01-27
*    > Minor cleanup

//  Create interaction variables for group comparisons

* DO: check if variales being generated already exist

capture program drop gdgen2
program define gdgen2, rclass
version 9

//  GET INFO FROM GLOBALS DEFINING GROUPS

    _gdsetcheck2 // do needed globals exist?
    local error = r(error)
    if `error'==1 {
        exit
    }

    local gvar  "$gvar"     // name of group variable
    local g1var "$g1var"    // variable equal to 1 for group 1
    local g0var "$g0var"    // variable equal to 1 for group 0

//  DECODE OPTIONS

    syntax varlist  /// variables for interactions
           [ ,      ///
           Details  /// list details of what is done
           ]

//  CREATE INTERACTIONS

    * create variables defining group 1 and 0
    gen `g1var' = (`gvar'==1) & `gvar'!=.
        label var `g1var' "(`gvar'==1)"
    gen `g0var' = (`gvar'==0) & `gvar'!=.
        label var `g0var' "(`gvar'==0)"

    * collect names of varibles created
    if "`details'"=="details" {
        local newvarlist "`g1var' `g0var'"
    }

    * create rhs interactions
    foreach v in `varlist' {

        foreach g in 0 1 {

            local newvarnm "`g`g'var'`v'" // g0varvar
            local newlblnm "(`gvar'==`g')*`v'"
            gen  `newvarnm' = ($gvar==`g')*`v'
                label var `newvarnm' "`newlblnm'"
            * collect names of variables created
            if "`details'"=="details" {
                local newvarlist "`newvarlist' `newvarnm'"
            }

        } // foreach g

    } // foreach v

//  DETAILS

    if "`details'"=="details" {
        di _new "These variables were generated:"
        codebook `newvarlist', compact
    }

end

exit

//  History

* gdxi1     0.1.0 2007-03-11
* gdxi1     0.1.1 2007-03-11 > Add global with rhs variables.
* gdxi1     0.1.2 2007-03-14 > RHS globals moved to gdxis2.ado.
* gdgenint1 0.1.3 2007-03-14 > Code cleanup and rename.
* gdgen2    0.2.0 2007-03-22 > Code cleanup and rename.