-1

我有以下do文件:

/* The following line should contain
   the complete path and name of the raw data file.
   On a PC, use backslashes in paths as in C:\  */   

local dat_name "/homes/data/cps-basic/jan10pub.dat"

/* The following line should contain the path to your output '.dta' file */

local dta_name "cpsb2010_1.dta"

/* The following line should contain the path to the data dictionary file */

local dct_name "cpsbjan10.dct"

/* The line below does NOT need to be changed */

quietly infile using "`dct_name'", using("`dat_name'") clear

/*------------------------------------------------

  All items, except those with one character, also can have values
  of -1, -2, or -3 even if the values are not in the documentation
  The meaning is
   -1 .Blank or not in universe
   -2 .Don't know
   -3 .Refused

  The following changes in variable names have been made, if necessary:
  '$' to 'd';            '-' to '_';              '%' to 'p';
  ($ = unedited data;     - = edited data;         % = allocated data)

  Decimal places have been made explict in the dictionary file.  
  Stata resolves a missing value of -1 / # of decimal places as a missing 
  -----------------------------------------------*/

** These note statements incorporate variable universes into the Stata data
note: by Jean Roth, jroth@nber.org Mon Feb 28 13:28:35 EST 2011
note hrmonth: U ALL HHLD's IN SAMPLE
note hryear4: U ALL HHLDs IN SAMPLE
note hurespli: U ALL HHLDs IN SAMPLE
note hufinal: U ALL HHLDs IN SAMPLE
note huspnish: U ALL HHLDs IN SAMPLE
note hetenure: U ALL HHLDs IN SAMPLE
note hehousut: U HRINTSTA = 1 OR HUTYPB = 1-3
note hetelhhd: U ALL HHLDs IN SAMPLE
note hetelavl: U HRINTSTA = 1
.... and so on. 

我有每月的数据,我可以运行这个程序来dta为每年的每个月创建文件。

但是,我每个月和每年都有几个原始数据。例如,我可以为 2010 年到2012 年的, , , ... 运行此do文件。我要为每个月获取一个文件,我必须在每次需要时更改它以用于不同的月份。JanFebMarchAprilAprildta

我想知道如何通过某种宏来实现这一点?

某种代码在代码周围添加循环,引用与该文件对应的每月文件的do文件名。

4

1 回答 1

2

我认为这样的事情会奏效。基本思想是循环两位数的年份和三个字符的月份,每次都重新定义当地人。由于您的 2012 年数据不完整,因此仅当存在年月组合输入文件时才会清除。您将需要编辑本地定义中的文件路径以反映您的目录结构。

cd "C:/Users/ziam/Desktop/CPS/"

local mons `=strlower(c(Mons))' // lowercase months (Jan -> jan) 

forvalues year = 10(1)12 {
    foreach month of local mons {

    local m : list posof "`month'" in mons // map jan to 1, feb to 2, ...

    capture confirm file "`year'`month'pub.dta" // check that raw file exists   

    /* Clean if file exits exists */
    if !_rc {
        local dat_name "`month'`year'pub.dat"
        local dta_name "cpsb20`year'_`m'.dta"
        local dct_name "`month'`year'.dct"
        quietly infile using "`dct_name'", using("`dat_name'") clear
        ...
    }

    /* Display message if file does not exist */
    else {
        di "No data for `month' in 20`year'"
    }

    }   
}
于 2015-11-18T21:32:34.603 回答