Stata Select Rows where Multiple Variables are All Observed, Jointly Nonmissing (DO, more see: Fan and Stata4Econ)
-----------------------------------------------------------------------------------------------------------------------------
name: select_rows_nonmissing
log: C:\Users\fan/Stata4Econ//summ/count/fs_nonmissing.smcl
log type: smcl
opened on: 6 Oct 2019, 18:48:32
. 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 Select Rows where Multiple Variables are All Observed, Jointly Nonmissing"
.
. ///--- Load Data
> set more off
. sysuse auto, clear
(1978 Automobile Data)
.
. ///--- Generating Index for Dropping
> set seed 987
. scalar it_drop_frac = 3
. gen row_idx_it = round((_n/_N)*it_drop_frac)
. gen row_idx_rand = round(it_drop_frac*uniform())
. replace mpg =. if row_idx_it == row_idx_rand
(21 real changes made, 21 to missing)
.
. set seed 123
. scalar it_drop_frac = 3
. replace row_idx_it = round((_n/_N)*it_drop_frac)
(0 real changes made)
. replace row_idx_rand = round(it_drop_frac*uniform())
(48 real changes made)
. replace price =. if row_idx_it == row_idx_rand
(14 real changes made, 14 to missing)
.
. ///--- list vars to include in a regression for example
> global svr_list "mpg price length weight"
.
. ///--- Conditioning
> global scd_bse "foreign !=."
. global scd_one "& foreign == 1"
. global scd_two "& gear_ratio <= 4"
.
. ///--- Drop approximately 1/2 of make randomly
> egen valid = rownonmiss($svr_list) if $scd_bse $scd_one $scd_two
(52 missing values generated)
.
. ///--- Tabulate and list Results
> tab valid
valid | Freq. Percent Cum.
------------+-----------------------------------
2 | 2 9.09 9.09
3 | 9 40.91 50.00
4 | 11 50.00 100.00
------------+-----------------------------------
Total | 22 100.00
. list $svr_list if valid == wordcount("$svr_list")
+--------------------------------+
| mpg price length weight |
|--------------------------------|
53. | 17 9,690 189 2,830 |
57. | 35 4,589 165 2,020 |
59. | 21 8,129 184 2,750 |
60. | 21 4,296 161 2,130 |
63. | 30 3,995 154 1,980 |
|--------------------------------|
64. | 14 12,990 192 3,420 |
66. | 35 3,798 164 2,050 |
69. | 18 5,719 175 2,670 |
70. | 23 7,140 172 2,160 |
71. | 41 5,397 155 2,040 |
|--------------------------------|
74. | 17 11,995 193 3,170 |
+--------------------------------+
.
. ///--- List including rows where not all values are observed but conditioning satisfied
> tab valid
valid | Freq. Percent Cum.
------------+-----------------------------------
2 | 2 9.09 9.09
3 | 9 40.91 50.00
4 | 11 50.00 100.00
------------+-----------------------------------
Total | 22 100.00
. list $svr_list if valid !=.
+--------------------------------+
| mpg price length weight |
|--------------------------------|
53. | 17 9,690 189 2,830 |
54. | . 6,295 174 2,070 |
55. | . . 177 2,650 |
56. | 23 . 170 2,370 |
57. | 35 4,589 165 2,020 |
|--------------------------------|
58. | . . 170 2,280 |
59. | 21 8,129 184 2,750 |
60. | 21 4,296 161 2,130 |
61. | 25 . 172 2,240 |
62. | 28 . 149 1,760 |
|--------------------------------|
63. | 30 3,995 154 1,980 |
64. | 14 12,990 192 3,420 |
65. | . 3,895 142 1,830 |
66. | 35 3,798 164 2,050 |
67. | . 5,899 174 2,410 |
|--------------------------------|
68. | 31 . 165 2,200 |
69. | 18 5,719 175 2,670 |
70. | 23 7,140 172 2,160 |
71. | 41 5,397 155 2,040 |
72. | . 4,697 155 1,930 |
|--------------------------------|
73. | . 6,850 156 1,990 |
74. | 17 11,995 193 3,170 |
+--------------------------------+
.
. ///--- End Log and to HTML
> log close _all
name: select_rows_nonmissing
log: C:\Users\fan/Stata4Econ//summ/count/fs_nonmissing.smcl
log type: smcl
closed on: 6 Oct 2019, 18:48:32
-----------------------------------------------------------------------------------------------------------------------------