Stata By Group Fill Missing Values by Nonmissing Values (DO, more see: Fan and Stata4Econ)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
      name:  stata_recode_discrete_subset
       log:  C:\Users\fan/Stata4Econ//gen/group/fs_group.smcl
  log type:  smcl
 opened on:   5 May 2020, 19:56:12


. log on $st_logname (log already on)

. . ///-- Site Link: Fan's Project Reusable Stata Codes Table of Content > di "https://fanwangecon.github.io/" https://fanwangecon.github.io/

. di "https://fanwangecon.github.io/Stata4Econ/" https://fanwangecon.github.io/Stata4Econ/

. . ///-- File Title > global filetitle "Stata By Group Fill Missing Values by Nonmissing Values"

. . ///--- Load Data > set more off

. sysuse auto, clear (1978 Automobile Data)

. . /////////////////////////////////////////////////////////////////////////////// > ///--- Fill Missing Values with NonMissing Min > /////////////////////////////////////////////////////////////////////////////// > . ///--- there are 18 trunk categories > codebook trunk

------------------------------------------------------------------------------------------------------------------------------------------------------------------- trunk Trunk space (cu. ft.) -------------------------------------------------------------------------------------------------------------------------------------------------------------------

type: numeric (int)

range: [5,23] units: 1 unique values: 18 missing .: 0/74

mean: 13.7568 std. dev: 4.2774

percentiles: 10% 25% 50% 75% 90% 8 10 14 17 20

. . * generate some random variable . gen var_one_val_in_group = uniform()

. . * keep one only value each group, all else null . * keep lowest weight length not null . bys trunk (weight length): replace var_one_val_in_group =. if _n != 1 (56 real changes made, 56 to missing)

. . * now populate this randomly selected value within each trunk group to all in group . * sort by var_test, the non-missing value shows up first . bys trunk (var_one_val_in_group): gen var_test_fill = var_one_val_in_group[1]

. . sort trunk price

. list trunk price weight length var_one_val_in_group var_test_fill, sepby(trunk)

+--------------------------------------------------------+ | trunk price weight length var_on~p var_te~l | |--------------------------------------------------------| 1. | 5 4,499 1,760 149 .3369739 .3369739 | |--------------------------------------------------------| 2. | 6 6,229 2,370 170 .357423 .357423 | |--------------------------------------------------------| 3. | 7 3,667 2,750 179 . .6775706 | 4. | 7 4,172 2,690 179 .6775706 .6775706 | 5. | 7 4,934 3,470 198 . .6775706 | |--------------------------------------------------------| 6. | 8 3,984 2,120 163 . .566447 | 7. | 8 4,589 2,020 165 .566447 .566447 | 8. | 8 5,079 2,280 170 . .566447 | 9. | 8 6,486 2,520 182 . .566447 | 10. | 8 8,129 2,750 184 . .566447 | |--------------------------------------------------------| 11. | 9 3,299 2,110 163 . .90876 | 12. | 9 3,748 2,200 165 . .90876 | 13. | 9 3,829 2,580 169 . .90876 | 14. | 9 4,389 1,800 147 .90876 .90876 | |--------------------------------------------------------| 15. | 10 3,895 1,830 142 .4374087 .4374087 | 16. | 10 4,187 2,650 179 . .4374087 | 17. | 10 4,195 2,730 180 . .4374087 | 18. | 10 4,453 2,230 170 . .4374087 | 19. | 10 5,799 2,240 172 . .4374087 | |--------------------------------------------------------| 20. | 11 3,798 2,050 164 . .8544706 | 21. | 11 3,995 1,980 154 . .8544706 | 22. | 11 4,099 2,930 186 . .8544706 | 23. | 11 4,425 1,800 157 .8544706 .8544706 | 24. | 11 4,647 3,260 170 . .8544706 | 25. | 11 4,749 3,350 173 . .8544706 | 26. | 11 5,719 2,670 175 . .8544706 | 27. | 11 6,295 2,070 174 . .8544706 | |--------------------------------------------------------| 28. | 12 3,799 2,640 168 . .6315421 | 29. | 12 7,140 2,160 172 .6315421 .6315421 | 30. | 12 9,735 2,650 177 . .6315421 | |--------------------------------------------------------| 31. | 13 3,955 3,430 197 . .7253782 | 32. | 13 4,082 3,400 200 .7253782 .7253782 | 33. | 13 4,424 3,420 203 . .7253782 | 34. | 13 15,906 4,290 204 . .7253782 | |--------------------------------------------------------| 35. | 14 4,181 3,370 200 . .742014 | 36. | 14 5,899 2,410 174 .742014 .742014 | 37. | 14 11,995 3,170 193 . .742014 | 38. | 14 12,990 3,420 192 . .742014 | |--------------------------------------------------------| 39. | 15 4,516 3,370 198 . .9405864 | 40. | 15 4,697 1,930 155 .9405864 .9405864 | 41. | 15 5,397 2,040 155 . .9405864 | 42. | 15 9,690 2,830 189 . .9405864 | 43. | 15 13,466 3,830 201 . .9405864 | |--------------------------------------------------------| 44. | 16 4,060 3,330 201 . .8831605 | 45. | 16 4,296 2,130 161 . .8831605 | 46. | 16 4,733 3,300 198 . .8831605 | 47. | 16 4,816 3,250 196 . .8831605 | 48. | 16 5,104 3,220 200 . .8831605 | 49. | 16 5,172 3,310 198 . .8831605 | 50. | 16 5,189 3,280 200 . .8831605 | 51. | 16 5,222 3,210 201 . .8831605 | 52. | 16 5,379 4,060 221 . .8831605 | 53. | 16 6,303 4,130 217 . .8831605 | 54. | 16 6,850 1,990 156 .8831605 .8831605 | 55. | 16 14,500 3,900 204 . .8831605 | |--------------------------------------------------------| 56. | 17 3,291 2,830 195 . .0850656 | 57. | 17 4,010 3,600 206 . .0850656 | 58. | 17 4,482 2,200 165 .0850656 .0850656 | 59. | 17 4,504 3,180 193 . .0850656 | 60. | 17 4,723 3,200 199 . .0850656 | 61. | 17 5,886 3,600 206 . .0850656 | 62. | 17 10,371 4,030 206 . .0850656 | 63. | 17 10,372 3,880 207 . .0850656 | |--------------------------------------------------------| 64. | 18 13,594 4,720 230 .2557557 .2557557 | |--------------------------------------------------------| 65. | 20 4,890 3,690 218 . .1477107 | 66. | 20 5,705 3,690 212 .1477107 .1477107 | 67. | 20 5,798 3,700 214 . .1477107 | 68. | 20 7,827 4,080 222 . .1477107 | 69. | 20 8,814 4,060 220 . .1477107 | 70. | 20 11,385 4,330 221 . .1477107 | |--------------------------------------------------------| 71. | 21 5,788 3,670 218 .7502809 .7502809 | 72. | 21 6,342 3,740 220 . .7502809 | |--------------------------------------------------------| 73. | 22 11,497 4,840 233 .337951 .337951 | |--------------------------------------------------------| 74. | 23 6,165 3,720 212 .7360283 .7360283 | +--------------------------------------------------------+

. . ///--- End Log and to HTML > log close _all name: stata_recode_discrete_subset log: C:\Users\fan/Stata4Econ//gen/group/fs_group.smcl log type: smcl closed on: 5 May 2020, 19:56:12 -------------------------------------------------------------------------------------------------------------------------------------------------------------------