		*_* bcoeffs 1.1.0 *_*


*--------------------------------------------

/*
*!version:1.1.0
*!author:Nisus
*!date:2016-4-25

*-ÉÏ´«µÄ°üÓÉÓÚÎÒÊèºöµ¼ÖÂ-beta()-±»ÎóÐ´Îª-generate()-
*-È¡ÏûÑ¡Ïî-dxmin-
*-Ôö¼Ó£º-moptions-ÓÃ»§¿ÉÒÔÖ¸¶¨»Ø¹éÄ£ÐÍµÄÑ¡Ïî£¬Èç-xtreg-Ê±ÓÃ-fe-»ò-re-



 */

*----------------

*!version:1.0.0
*!author:Nisus
*!date:2015-5-24

/* ¸Ä½ø·½·¨£º
	¶Ô×Ô±äÁ¿À´¸öÖð¸öµü´ú
	½«Ô­ bcoeff Î¢µ÷ÏÂ¡£¡£---2015-5-24 01:20:43
	¿ÉÒÔµÃµ½Èô¸É¸ö×Ô±äÁ¿µÄÏµÊý£¬¶ø²»ÊÇÒ»¸ö±äÁ¿µÄ¡£¡£¡£

	ÈôÒªµÃµ½ x1 µÄÏµÊý¡¢±ê×¼Îó¡¢¡¢£¬´¢´æÔÚ±äÁ¿£º_b_x1 ¡¢ _se_x1  _cons

 */
	// Ö÷ÒªÓÃÍ¾£º´¢´æ¸É±äÁ¿µÄ¹À¼ÆÏµÊý£¡ ²Î¼û£º-bcoeff-



*============================================================

cap pro drop bcoeffs
program define bcoeffs
*------bcoeff's syntax below-------
/* version 13.0
syntax varlist(min=2 numeric) [if] [in] , Generate(str) ///
		[by(varlist) Se(str) Cons(str) Double MISSing]

program define bcoeff
*! 1.2.0 ZW/NJC 20 June 2000
* 1.1.0 NJC 15 June 2000
* after deltaco 1.0.5 Z.WANG 16Jun2000
* deltaco 1.0.1 Z.WANG 1May1999 */
*-------------------------------------------
        version 13.0
        syntax varlist(min=2 numeric)  /*
        */ [, Beta(str) by(varlist) /*
        */  Model(str)  MOptions(str)  Nmin(real 2)  Only(varlist numeric)/*
        */ Se(str) Cons(str) Double MISSing ]
        	// v1.1.0?¨®¨¨???1¨¦?¡êD¨ª????¡êo-MOption


		marksample touse

    quietly {

        *--´íÎóÌáÊ¾---------------------------------

            *- ËäÈ»ËùÓÐÑ¡Ïî¶¼ÊÇ¿ÉÑ¡Ïî£¬µ«ÊÇÊ²Ã´Ñ¡Ïî¶¼²»Ñ¡£¬ÄãÏë×öÊ²Ã´ÄØ£¿
            if "`beta'"=="" & "`se'"=="" & "`cons'"==""  {
            	dis in red "You should indicate whcih coefficient to store, {opt beta /se /sons ?}"
            	exit 198
            }

		*--³õÊ¼ÉèÖÃ-----------------------
	        local model = cond(trim(`"`model'"') == "", "regress", "`model'")   //model -- regress y x1 x2 ...
	       /*  if "`dxmin'" == "" {
				local dxmin = 0
			}    (v1.1.0  cancel)*/

			local moptions=cond(trim(`"`moptions'"')=="" , "" , ",`moptions'") // (v1.1.0 add) -- options of regression, like xtreg's fe or re

            *- get independent varible whose coeffient to store  and how many ... -- nvar
            if "`only'"==""  {
				gettoken y x : varlist

				local nvar : word count `x'

				tokenize `x'  //split inpen...

			}
			else {
				local nvar : word count `only'
				tokenize `only'

			}

			*- by option
			if "`by'" != "" {
				loc bylab "by(`by')"
			}


	*--gen null variable for store ... and labels----------------

		forval var=1/`nvar'  {


				*- regress and store coeffients

                if "`cons'" != ""  {

					if `var'==1  {
						local xcons "__`cons'"
                        confirm new variable `xcons'
                        gen `double' `xcons' = .
                        local lbl /*
                        */ `"constant: `model' `varlist'`if' `in' `moptions'`bylab'"'
                        if length("`lbl'") > 80 {
                                note `xcons' : `"`lbl'"'
                                label var `xcons' "see notes->type cmd:note"
                        }
                        else  {
							label var  `xcons'  `"`lbl'"'
						}
					}
				}
				else loc nocons "*"


                *- beta
                if "`beta'" !="" {


						confirm new variable  _`beta'_``var''
						local xb "_`beta'_``var''"
						gen `double' `xb' = .
						local lbl `"b[``var'']: `model' `varlist'`ifn' `moptions'`bylab'"'
						if length("`lbl'") > 80 {
	                        note `xb' : `"`lbl'"'
	                        label var `xb' "see notes->type cmd:note"
						}
						else {
							dis in r "`inx'"
							label var `xb' `"`lbl'"'
						}

					}
				else local nobeta "*"

        		*- se
                if "`se'" != "" {
						local xse "_`se'_``var''"
                        confirm new variable `xse'
                        gen `double' `xse' = .
						local lbl /*
                        */ `"se[``var'']: `model' `varlist'`ifn' `moptions'`bylab'"'
                        if length("`lbl'") > 80 {
                                note `xse' : `"`lbl'"'
                                label var `xse' "see notes->type cmd:note"
                        }
                        else label var `xse' `"`lbl'"'
                }
                else local nose "*"



		}  //forval  end




	*--group to regress ,...---------



				tempvar  group

                sort `touse' `by'
                by `touse' `by': replace `touse' = 0 if _N < `nmin'  //exclude obs <[nimin]
               /*  egen `xmax' = max(`x1'), by(`touse' `by')
                egen `xmin' = min(`x1'), by(`touse' `by')
                replace `touse' = 0 if (`xmax' - `xmin') <= `dxmin'  */

                egen `group' = group(`by') if `touse', `missing'
                su `group', meanonly
                local ng = r(max)  //get count of groups
				if `ng'>50 {
					dis in green "Note:count of goups is greater than 50, would run slowly."
				}

                local i = 1
                while `i' <= `ng' {
                        cap `model' `varlist' if `group' == `i'  `moptions'   //     EX:   xtreg y x if group == i & ... in ... , fe
						//dis in r "`model' `varlist' if `group' == `i'"
                        if _rc == 0 {


                        	forval var=1/`nvar'   {


								`nose' replace _`se'_``var'' = /*
	                                */ _se[``var''] if `group' == `i'

	                            `nocons' if `var'==1   replace __`cons' = /*
	                                */ _b[_cons] if `group' == `i'

	                            `nobeta' replace _`beta'_``var'' = _b[``var''] if `group' == `i'

                        	}

                        }
                        local i = `i' + 1
                }



}  //quietly end




end










/* test */

/*

cls
sysuse auto.dta , clear
set trace on

bcoeffs_t price mpg , b(b) se(s) c(c)  by(foreign)  //ok
bcoeffs_t price mpg trunk weight length, b(b) se(s) c(c)  by(foreign rep78) only(weight length)  //ok
bcoeffs_t price mpg trunk weight length, b(b) se(s) c(c)  by(foreign rep78) only(weight length)  nmin(4)  //ok



bcoeffs_t price mpg , b(b) se(s) c(c)  //ok
set trace on
bcoeffs_t price mpg , b(b)  // ok

drop _*
bcoeffs_t price mpg , b(b) se(s)  //ok

cls
sysuse census.dta ,clear
bysort region:gen year=_n
xtset region year
set trace on
bcoeffs_t death pop medage , beta(b) model(xtreg) moption(fe)

sysuse census.dta ,clear
bysort region:gen year=_n
xtset region year
gen gr=.
replace gr=1 in 1/10
replace gr=2 in 11/15
replace gr=3 in 16/40
replace gr=4 in 41/50
set trace on
bcoeffs_t death pop medage , beta(b) se(se) cons(c) model(xtreg) moption(re) by(gr) // ÓÐµÄ×é±äÁ¿²»¹»

*/