Code to deal with missing values
Final version, including output to database table