开始之前

  • 首先要明确自己的数据清洗需求,我的需求如下:
    • 构建一个面板数据,其中需要包含所有我需要的变量。
  • 其次是拆分需求:
      1. 面板数据需要由截面数据来合并;
      1. 变量需要的保留与否暂不清晰,但根据已读文献,有初步方向。
      1. 确定需要使用的数据库,每个年份的数据集有如下几个:
        • famconf(家庭成员关系数据库)
      • famecon(家庭经济库)
      • person(个人库)
      • childproxy(少儿家长代答库)

开始清洗

制定根目录

  • 为了方便合作清洗/给他人检查代码,先确定目录如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
clear all
clear matrix
set more off

global root = "F:\DocFiles\Working Area\mental health"
global origin_data = "$root\Origin_data"
global cfps2020 = "$origin_data\CFPS2022"
global cfps2020 = "$origin_data\CFPS2020"
global cfps2018 = "$origin_data\CFPS2018"
global cfps2016 = "$origin_data\CFPS2016"
global cfps2014 = "$origin_data\CFPS2014"
global cfps2012 = "$origin_data\CFPS2012"
global dofiles = "$root\Working_Files\Do_Files"
global logfiles = "$root\Working_Files\Log_Files"
global datafiles = "$root\Working_Files\Data_Files"
global temp_data = "$root\Working_Files\Temp_Files"
log using "$logfiles\mental_health_clean.log", replace

清洗截面数据

筛选变量

  • 首先清洗2012年数据,用keep指令保留所有需要的变量
1
2
3
4
5
6
7
8
*-----------------------2012年--------------------------------
*- 提取变量
use "$cfps2012/cfps2012adult_201906.dta", clear
keep pid fid12 fid10 provcd countyid cid urban12

gen year = 2012
gen age = year - cfps2012_birthy_best //年龄
label variable 年龄

考虑到只靠do文件清理变量会面临一些问题:

  1. 变量太多,很容易乱
  2. 无法核对我需要的变量是否已清洗
  3. 无法核对已清洗的变量和问卷中被keep掉的变量

故而决定采用表格来整理已经清洗的变量

  • 在excel表格构建表格如下
变量 变量名称 变量定义 2012 2014 ……
cig 是否吸烟 是否吸烟 qq201
cig_num 吸烟数量 每天吸多少支 qq202
cig_quit 是否曾经戒烟 是否曾经戒烟 qq204
cig_quit_age 戒烟年龄 戒烟年龄 qq205
  • 变量名为rename之后的变量名称,即用在后续回归中的变量名称;
  • 变量定义为问卷中变量的定义/自己构建的变量定义;
  • 其中年列代表每年问卷中的问题代码。