我有以下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
文件。
但是,我每个月和每年都有几个原始数据。例如,我可以为do
,Jan
,Feb
,March
...运行此April
文件,直到April
为止对于我来说,每个月都要获得一个dta
文件,每次我想要一个不同的月份时,我都要去更改它。
我想知道如何通过某种宏来实现这一目标?
在代码周围添加循环的某种代码,引用与该do
文件对应的每月文件的文件名。
答案 0 :(得分: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'"
}
}
}