File size: 2,285 Bytes
3fb2143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
*! version 1.2 09-OCT-2022 
 
* Generalized irecode function

program define binsreg_irecode
   version 13
   
   syntax varlist(max=1 numeric) [if] [in], knotmat(name) BINid(varname numeric) ///
                                            [usegtools nbins(string) pos(string) knotliston(string)] 
   
   /* knot is a FULL knot matrix with boundaries */
   /* used internally, no error checks */
   
   marksample touse
   
   if ("`usegtools'"==""|"`pos'"!="QS") {
      confirm variable `binid'
	  confirm matrix `knotmat'
   
      local n=rowsof(`knotmat')
      if (`n'==2) qui replace `binid'=1 if `touse'
      else if (`n'==3) qui replace `binid'=1+irecode(`varlist', `knotmat'[2,1]) if `touse'
      else {
         local J = `n' - 2
         local knots = `knotmat'[2,1]
         local assignedbins = 1
	     local knotstsize : strlen local knots
	     local knotsnextsize = 0

         forvalues j = 2/`J' {
	        local knotsnext = `knotmat'[`j'+1,1]
	        local knotstsize = `knotstsize' + `knotsnextsize'
	        local knotsnextsize : strlen local knotsnext

	        if (`knotstsize' + `knotsnextsize' < `c(macrolen)') & (`j' - `assignedbins' < 248){
		       local knots "`knots',`knotsnext'"
		       if `j' == `J' {
			      qui replace `binid' = `assignedbins' + irecode(`varlist',`knots') if `touse' & `binid'==.
		       }
	        }
	        else {
		       qui replace `binid' = `assignedbins' + irecode(`varlist',`knots') if `binid'==. & `touse'
		       if `j'<`J' {
			     qui replace `binid' = . if `varlist' > `knotmat'[`j'+1,1] & `touse'
			     local knots = `knotmat'[`j'+1,1]
			     local assignedbins  = `j'
			     local knotstsize = 0
		       }
			   else {
			     qui replace `binid' = `n'-1 if `varlist' > `knotmat'[`j'+1,1] & `touse'
			   }
	        }
         } 
      }
   }
   else {
      capture confirm variable `binid'
	  if (!_rc) drop `binid'
	  
      if (`nbins'==1) qui replace `binid'=1 if `touse' 
      else {
	     tempvar cat
         if ("`knotliston'"=="T") fasterxtile `binid'=`varlist', nq(`nbins')
		 else                     fasterxtile `binid'=`varlist'  if `touse', nq(`nbins')
		 *qui replace `binid'=`cat' if `touse'
	  }
   }
   
   
end