version: 1.0
help fillmissing May 11, 2020

Title

fillmissing - Fills missing values in a variable based on a given
criterion

Syntax

[bysort varlist]: fillmissing
varname [if] [in],
[with(with_options)]

Important note:

This program does not imply that filling missing values is justified
by theory. Users should make their own decisions and follow appropriate theory
while filling missing values.

Description

fillmissing program provides the convenience of filling missing
values in numeric or string variables with the variable's previous value, forward
value, first or last value, or with some calculated values such as mean, median,
minimum, maximum, etc. in numeric variables. The program can work with
or without the bysort prefix. Specifically, if filling the missing values is
needed within a grouping variable, such as firms, years, etc, the bysort prefix can
be used. See examples below.

Options

1. with(): Option with() is used to specify the source from where
the missing values will be filled. It accepts the following arguments:

Argument Purpose

with(any) with(any) is an optional option and hence if not
specified, will automatically be invoked by the fillmissing program.
This option is best to fill missing values of a constant variable,
i.e. a variable that has all similar values, however, due to some
reason, some of the values are missing. Option with(any) will try
to fill the missing values from any available non-missing values
of the given variable.

with(previous) Option with(previous) is used to fill the current
missing value with the preceding or previous value of the same variable.
Please note that if the previous value is also missing, the current
value will remain missing. Further, this option does not sort the data,
so whatever the current sort of the data is, fillmissing will use that
sort and identify the current and the previous observation.

with(next) with(next)} works like with(previous)
with the difference that it tries to fill the current
missing value with the following or next value of the same variable.
Please note again if the next value is also missing, the current
value will remain missing. Further, this option does not sort the data,
so whatever the current sort of the data is, fillmissing will use that
sort and identify the current and the next observation.

with(first) It will replace all missing values with the first
value in the current sort or the first value of each group when used
with the bysort prefix.

with(last) It will replace all missing values with the last
value in the current sort or the last value of each group when used
with the bysort prefix.

with(mean) It will replace missing values with the mean value of non-missing values. This option can be used only with numeric variables.

with(median) It will replace missing values with the median value of non-missing values. This option can be used only with numeric variables.

with(min) It will replace missing values with the minimum value of non-missing values. This option can be used only with numeric variables.

with(max) It will replace missing values with the maximum value of non-missing values. This option can be used only with numeric variables.

2. bysort varlist: The bysort varlist prefix can be used to
perform calculations over grouping variables. See examples below.

Examples

Example 1: Fill missing values with(any)

Let us first create a sample dataset of one variable having 10 observations.
You can copy-paste the following code to Stata Do editor to generate the dataset

clear all
set obs 10
gen symbol = "AABS"
replace symbol = "" in 5
replace symbol = "" in 8

The above dataset has missing values on row 5 and 8. To fill the
missing values from any other available non-missing values, let
us use the with(any) option.

fillmissing symbol, with(any)

Since with(any) is the default option of the program, we could also write the above code as

fillmissing symbol

Example 2: Fill missing values with(previous)

Let’s create a dummy dataset first.

clear all
set obs 10
gen symbol = "AABS"
replace symbol = "AKBL" in 1
replace symbol = "" in 2

To fill the missing value in observation number 2 with AKBL, i.e.
from previous observation, we would type:

fillmissing symbol, with(previous)

Example 3: Fill missing values within groups

. webuse grunfeld

Create some missing values and fill them within the grouping variable company

replace kstock = . in 2

replace kstock = . in 25

bysort company: fillmissing kstock)

Web Page

For more examples, comments, questions/answers, please visit this page

fillmissing: Fill Missing Values in Stata

Author

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *
* *
* Dr. Attaullah Shah *
* Institute of Management Sciences, Peshawar, Pakistan *
* Email: attaullah.shah@imsciences.edu.pk *
* www.StataProfessor.com *
* www.OpenDoors.Pk *
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*

Also see

asdoc,
astile,
ascol,
asreg,
asm,
astx,
searchfor.