I have several .Dat files and each file is for a seperate month and seperate year. The years range is 2010 to 2012, and the months are Jan-Dec for 2010, Jan-Dec for 2011, and Jan-Apr for 2012. Again, I have a seperate .DAT file for each month of each year. I can obviously edit the program separately to run for each month of each year and create a monthly .dta files for each, however, I wanted to create a macro that would accomplish this without having to change the .do files every time I run it for a different .dat file.
So for example, the following top part creates a .dta file for the month of January 2010.
local dat_name "C:\Users\ziam\Desktop\CPS\jan10pub.dat"
/* The following line should contain the path to your output '.dta' file */
local dta_name "C:\Users\ziam\Desktop\CPS\DtaFiles\cpsb2010_1 .dta "
/* The following line should contain the path to the data dictionary file */
local dct_name "I:\CPS Data\.dct files\cpsbjan10.dct"
/* The line below does NOT need to be changed */
quietly infile using "`dct_name'", using("`dat_name'") clear
And this works perfectly fine. However, I used the following code in hoping of creating a separte .dta file for each month of year without having to change the code over and over again.
The Macro Program (the part in red is where i have the macro code)
set mem 500m
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 "C:\Users\ziam\Desktop\CPS\DtaFiles\`year'`mon th'p ub.dta" // check that raw file exists
/* Clean if file exits exists */
if !_rc {
local dat_name "C:\Users\ziam\Desktop\CPS\`month'`year'pub.da t"
local dta_name "C:\Users\ziam\Desktop\CPS\DtaFiles\cpsb20`yea r'_` m'.dta"
local dct_name "I:\CPS Data\.dct files\cpsb`month'`year'.dct"
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 value.
-----------------------------------------------*/
** These note statements incorporate variable universes into the Stata data file.
note: by Jean Roth, [email protected] 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
note hephoneo: U HETELHHD = 2
note hefaminc: U HETELHHD = 1 OR HETELAVL = 1
note hutypea: U HETELHHD = 1 OR HETELAVL = 1
note hutypb: U HETELHHD = 1 OR HETELAVL = 1
note hutypc: U HETELHHD = 1 OR HETELAVL = 1
note hwhhwgt: U HETELHHD = 1 OR HETELAVL = 1
note hrintsta: U HRINTSTA = 1
note hrnumhou: U ALL HHLDs IN SAMPLE
note hrhtype: U ALL HHLDs IN SAMPLE
note hrmis: U ALL HHLDs IN SAMPLE
note huinttyp: U ALL HHLDs IN SAMPLE
note huprscnt: U ALL HHLDs IN SAMPLE
note hrlonglk: U ALL HHLDs IN SAMPLE
note hrhhid2: U ALL HHLDs IN SAMPLE
note hubus: U ALL HHLD's IN SAMPLE
note hubusl1: U ALL HHLD's IN SAMPLE
note hubusl2: U ALL HHLD's IN SAMPLE
note hubusl3: U ALL HHLD's IN SAMPLE
note hubusl4: U ALL HHLD's IN SAMPLE
note gereg: U ALL HHLD's IN SAMPLE
note gestcen: U ALL HHLD's IN SAMPLE
note gestfips: U ALL HHLD's IN SAMPLE
note gtcbsa: U ALL HHLD's IN SAMPLE
note gtco: U ALL HHLD's IN SAMPLE
note gtcbsast: U ALL HHLD's IN SAMPLE
note gtmetsta: U ALL HHLD's IN SAMPLE
note gtindvpc: U ALL HHLD's IN SAMPLE
note gtcbsasz: U ALL HHLD's IN SAMPLE
note gtcsa: U ALL HHLD's IN SAMPLE
note proldrrp: U ALL HHLD's IN SAMPLE
note pupelig: U PRPERTYP = 1, 2, OR 3
note perrp: U PRPERTYP = 1, 2, OR 3
note peparent: U PRPERTYP = 1, 2, OR 3
note peage: U EVERY PERSON
note prtfage: U PRPERTYP = 1, 2, 0R 3
note pemaritl: U PRPERTYP = 1, 2, 0R 3
note pespouse: U PEAGE >= 15
note pesex: U PEMARITL = 1
note peafever: U PRPERTYP = 1, 2, 0R 3
note peafnow: U PEAGE >=17
note peeduca: U PRPERTYP = 2 or 3
note ptdtrace: U PRPERTYP = 2 0R 3
note prdthsp: U PRPERTYP = 1, 2, 0R 3
note puchinhh: U PEHSPNON = 1
note pulineno: U PEHSPNON = 1
note prfamnum: U PEHSPNON = 1
note prfamrel: U PRPERTYP = 1, 2, 0R 3
note prfamtyp: U PRPERTYP = 1, 2, 0R 3
note pehspnon: U PRPERTYP = 1, 2, 0R 3
note prmarsta: U PRPERTYP = 1, 2, 0R 3
note prpertyp: U PRPERTYP = 2 0R 3
note penatvty: U ALL HOUSEHOLD MEMBERS
note pemntvty: U PRPERTYP = 1, 2, 0R 3
note pefntvty: U PRPERTYP = 1, 2, 0R 3
note prcitshp: U PRPERTYP = 1, 2, 0R 3
note prcitflg: U PRPERTYP = 1, 2, 0R 3
note prinusyr: U PRPERTYP = 1, 2, 0R 3
note puslfprx: U PRCITSHP = 2, 3, 4, OR 5
note pemlr: U PRCITSHP = 2, 3, 4, OR 5
note puwk: U PRPERTYP = 2
note pubus1: U PRPERTYP = 2
note pubus2ot: U PRPERTYP = 2
note pubusck1: U PRPERTYP = 2
note pubusck2: U PRPERTYP = 2
note pubusck3: U PRPERTYP = 2
note pubusck4: U PRPERTYP = 2
note puretot: U PRPERTYP = 2
note pudis: U PRPERTYP = 2
note peret1: U PRPERTYP = 2
note pudis1: U PEMLR = 5 AND (PURETOT = 1 OR
note pudis2: U PEMLR = 5 AND (PURETOT = 1 OR
note puabsot: U PEMLR = 5 AND (PURETOT = 1 OR
note pulay: U PEMLR = 5 AND (PURETOT = 1 OR
note peabsrsn: U PEMLR = 5 AND (PURETOT = 1 OR
note peabspdo: U PEMLR = 2
note pemjot: U PEABSRSN = 4-12, 14
note pemjnum: U PEMLR = 1, 2
note pehrusl1: U PEMJOT = 1
note pehrusl2: U PEMJOT = 1 OR 2 AND PEMLR = 1 OR 2
note pehrftpt: U PEMJOT = 1 AND PEMLR = 1 OR 2
note pehruslt: U PEHRUSL1 = -4 OR PEHRUSL2 = -4
note pehrwant: U PEMLR = 1 OR 2
note pehrrsn1: U PEMLR = 1 AND
note pehrrsn2: U PEHRWANT = 1 (PEMLR = 1 AND PEHRUSLT < 35)
*Everything below this point, aside from the final save, are value labels
#delimit ;
;
label values hufinal hufinal;
label define hufinal
1 "FULLY COMPLETE CATI INTERVIEW"
2 "PARTIALLY COMPLETED CATI"
........and so on until I close the loop below
compress
saveold `dta_name' , replace
}
/* Display message if file does not exist */
else {
di "No data for `month' in 20`year'"
}
}
}
However, my program doesn't create the .dta files and the do file just keeps running: What am I doing wrong? Am I closing the loop incorrectly?
The following log displays while my program continues to run:
. /* Display message if file does not exist
> */
. else {
586. di "No data for `month' in 20`year'
> "
587. }
588.
. }
589. }
No data for jan in 2010
No data for feb in 2010
No data for mar in 2010
No data for apr in 2010
No data for may in 2010
No data for jun in 2010
No data for jul in 2010
No data for aug in 2010
No data for sep in 2010
No data for oct in 2010
No data for nov in 2010
No data for dec in 2010
No data for jan in 2011
No data for feb in 2011
No data for mar in 2011
No data for apr in 2011
No data for may in 2011
No data for jun in 2011
No data for jul in 2011
No data for aug in 2011
No data for sep in 2011
No data for oct in 2011
No data for nov in 2011
No data for dec in 2011
No data for jan in 2012
No data for feb in 2012
No data for mar in 2012
No data for apr in 2012
So for example, the following top part creates a .dta file for the month of January 2010.
local dat_name "C:\Users\ziam\Desktop\CPS\jan10pub.dat"
/* The following line should contain the path to your output '.dta' file */
local dta_name "C:\Users\ziam\Desktop\CPS\DtaFiles\cpsb2010_1 .dta "
/* The following line should contain the path to the data dictionary file */
local dct_name "I:\CPS Data\.dct files\cpsbjan10.dct"
/* The line below does NOT need to be changed */
quietly infile using "`dct_name'", using("`dat_name'") clear
And this works perfectly fine. However, I used the following code in hoping of creating a separte .dta file for each month of year without having to change the code over and over again.
The Macro Program (the part in red is where i have the macro code)
set mem 500m
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 "C:\Users\ziam\Desktop\CPS\DtaFiles\`year'`mon th'p ub.dta" // check that raw file exists
/* Clean if file exits exists */
if !_rc {
local dat_name "C:\Users\ziam\Desktop\CPS\`month'`year'pub.da t"
local dta_name "C:\Users\ziam\Desktop\CPS\DtaFiles\cpsb20`yea r'_` m'.dta"
local dct_name "I:\CPS Data\.dct files\cpsb`month'`year'.dct"
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 value.
-----------------------------------------------*/
** These note statements incorporate variable universes into the Stata data file.
note: by Jean Roth, [email protected] 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
note hephoneo: U HETELHHD = 2
note hefaminc: U HETELHHD = 1 OR HETELAVL = 1
note hutypea: U HETELHHD = 1 OR HETELAVL = 1
note hutypb: U HETELHHD = 1 OR HETELAVL = 1
note hutypc: U HETELHHD = 1 OR HETELAVL = 1
note hwhhwgt: U HETELHHD = 1 OR HETELAVL = 1
note hrintsta: U HRINTSTA = 1
note hrnumhou: U ALL HHLDs IN SAMPLE
note hrhtype: U ALL HHLDs IN SAMPLE
note hrmis: U ALL HHLDs IN SAMPLE
note huinttyp: U ALL HHLDs IN SAMPLE
note huprscnt: U ALL HHLDs IN SAMPLE
note hrlonglk: U ALL HHLDs IN SAMPLE
note hrhhid2: U ALL HHLDs IN SAMPLE
note hubus: U ALL HHLD's IN SAMPLE
note hubusl1: U ALL HHLD's IN SAMPLE
note hubusl2: U ALL HHLD's IN SAMPLE
note hubusl3: U ALL HHLD's IN SAMPLE
note hubusl4: U ALL HHLD's IN SAMPLE
note gereg: U ALL HHLD's IN SAMPLE
note gestcen: U ALL HHLD's IN SAMPLE
note gestfips: U ALL HHLD's IN SAMPLE
note gtcbsa: U ALL HHLD's IN SAMPLE
note gtco: U ALL HHLD's IN SAMPLE
note gtcbsast: U ALL HHLD's IN SAMPLE
note gtmetsta: U ALL HHLD's IN SAMPLE
note gtindvpc: U ALL HHLD's IN SAMPLE
note gtcbsasz: U ALL HHLD's IN SAMPLE
note gtcsa: U ALL HHLD's IN SAMPLE
note proldrrp: U ALL HHLD's IN SAMPLE
note pupelig: U PRPERTYP = 1, 2, OR 3
note perrp: U PRPERTYP = 1, 2, OR 3
note peparent: U PRPERTYP = 1, 2, OR 3
note peage: U EVERY PERSON
note prtfage: U PRPERTYP = 1, 2, 0R 3
note pemaritl: U PRPERTYP = 1, 2, 0R 3
note pespouse: U PEAGE >= 15
note pesex: U PEMARITL = 1
note peafever: U PRPERTYP = 1, 2, 0R 3
note peafnow: U PEAGE >=17
note peeduca: U PRPERTYP = 2 or 3
note ptdtrace: U PRPERTYP = 2 0R 3
note prdthsp: U PRPERTYP = 1, 2, 0R 3
note puchinhh: U PEHSPNON = 1
note pulineno: U PEHSPNON = 1
note prfamnum: U PEHSPNON = 1
note prfamrel: U PRPERTYP = 1, 2, 0R 3
note prfamtyp: U PRPERTYP = 1, 2, 0R 3
note pehspnon: U PRPERTYP = 1, 2, 0R 3
note prmarsta: U PRPERTYP = 1, 2, 0R 3
note prpertyp: U PRPERTYP = 2 0R 3
note penatvty: U ALL HOUSEHOLD MEMBERS
note pemntvty: U PRPERTYP = 1, 2, 0R 3
note pefntvty: U PRPERTYP = 1, 2, 0R 3
note prcitshp: U PRPERTYP = 1, 2, 0R 3
note prcitflg: U PRPERTYP = 1, 2, 0R 3
note prinusyr: U PRPERTYP = 1, 2, 0R 3
note puslfprx: U PRCITSHP = 2, 3, 4, OR 5
note pemlr: U PRCITSHP = 2, 3, 4, OR 5
note puwk: U PRPERTYP = 2
note pubus1: U PRPERTYP = 2
note pubus2ot: U PRPERTYP = 2
note pubusck1: U PRPERTYP = 2
note pubusck2: U PRPERTYP = 2
note pubusck3: U PRPERTYP = 2
note pubusck4: U PRPERTYP = 2
note puretot: U PRPERTYP = 2
note pudis: U PRPERTYP = 2
note peret1: U PRPERTYP = 2
note pudis1: U PEMLR = 5 AND (PURETOT = 1 OR
note pudis2: U PEMLR = 5 AND (PURETOT = 1 OR
note puabsot: U PEMLR = 5 AND (PURETOT = 1 OR
note pulay: U PEMLR = 5 AND (PURETOT = 1 OR
note peabsrsn: U PEMLR = 5 AND (PURETOT = 1 OR
note peabspdo: U PEMLR = 2
note pemjot: U PEABSRSN = 4-12, 14
note pemjnum: U PEMLR = 1, 2
note pehrusl1: U PEMJOT = 1
note pehrusl2: U PEMJOT = 1 OR 2 AND PEMLR = 1 OR 2
note pehrftpt: U PEMJOT = 1 AND PEMLR = 1 OR 2
note pehruslt: U PEHRUSL1 = -4 OR PEHRUSL2 = -4
note pehrwant: U PEMLR = 1 OR 2
note pehrrsn1: U PEMLR = 1 AND
note pehrrsn2: U PEHRWANT = 1 (PEMLR = 1 AND PEHRUSLT < 35)
*Everything below this point, aside from the final save, are value labels
#delimit ;
;
label values hufinal hufinal;
label define hufinal
1 "FULLY COMPLETE CATI INTERVIEW"
2 "PARTIALLY COMPLETED CATI"
........and so on until I close the loop below
compress
saveold `dta_name' , replace
}
/* Display message if file does not exist */
else {
di "No data for `month' in 20`year'"
}
}
}
However, my program doesn't create the .dta files and the do file just keeps running: What am I doing wrong? Am I closing the loop incorrectly?
The following log displays while my program continues to run:
. /* Display message if file does not exist
> */
. else {
586. di "No data for `month' in 20`year'
> "
587. }
588.
. }
589. }
No data for jan in 2010
No data for feb in 2010
No data for mar in 2010
No data for apr in 2010
No data for may in 2010
No data for jun in 2010
No data for jul in 2010
No data for aug in 2010
No data for sep in 2010
No data for oct in 2010
No data for nov in 2010
No data for dec in 2010
No data for jan in 2011
No data for feb in 2011
No data for mar in 2011
No data for apr in 2011
No data for may in 2011
No data for jun in 2011
No data for jul in 2011
No data for aug in 2011
No data for sep in 2011
No data for oct in 2011
No data for nov in 2011
No data for dec in 2011
No data for jan in 2012
No data for feb in 2012
No data for mar in 2012
No data for apr in 2012
Comment