-1

I have the following do file:

/* 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. 

I have monthly data and I can run this program to create the dta file for each month of year.

However, I have several raw data for each month and year. For example, I can run this do file for Jan, Feb, March, April... for the year 2010 until April 2012. For me to get a dta file for each month, I have to go and change it every time I want it for a different month.

I would like to know how one can accomplish this via some sort of a macro?

Some sort of code to add loops around the code referring to the filenames of the monthly files corresponding to that do file.

mustafghan
  • 169
  • 4
  • 15
  • You can start your research with `help foreach`. – Roberto Ferrer Nov 18 '15 at 21:00
  • Cross-posted at http://www.statalist.org/forums/forum/general-stata-discussion/general/1319231-macro-to-create-several-monthly-dta-files Telling people about cross-posting never hurts and often helps avoid duplicated effort. The fact that Statalist is a quite different forum with only some members in common with SO makes this more important, not less. – Nick Cox Dec 07 '15 at 18:43

1 Answers1

2

I think something like this will do the trick. The basic idea is to loop over two-digit years and three-character months, re-defining the locals each time. Since your 2012 data is incomplete, this will only clean if the year-months combo input file exists. You will need to edit the file paths in the local definitions to reflect your directory structure.

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'"
    }

    }   
}
dimitriy
  • 9,077
  • 2
  • 25
  • 50
  • So basically the only thing I have to change is the directory name? Do I have to type in the name of each individual months? Also, should the macro wrap around the entire code? – mustafghan Dec 07 '15 at 03:49
  • Yes (the full path), no, and yes. – dimitriy Dec 07 '15 at 07:40
  • So I had done actually done exactly that, but .do file just continues running and I see the (partial) log with the following message: /* 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 and so on..... The path directories are all correct, because I run the code without the macro and it creates the individual monthly .dta files just fine. – mustafghan Dec 07 '15 at 11:30
  • This is how I have it: 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 capture confirm file "C:\Users\ziam\Desktop\CPS\DtaFiles\`year'`month'pub.dta" if !_rc { local dat_name "C:\Users\ziam\Desktop\CPS\`month'`year'pub.dat" local dta_name "C:\Users\ziam\Desktop\CPS\DtaFiles\cpsb20`year'_`m'.dta"local dct_name "I:\CPS Data\.dct files\cpsb`month'`year'.dct" quietly infile using "`dct_name'", using("`dat_name'") clear – mustafghan Dec 07 '15 at 15:05
  • As pointed out in the discussion on Statalist at http://www.statalist.org/forums/forum/general-stata-discussion/general/1319231-macro-to-create-several-monthly-dta-files the back slashes in the path names will prevent macros immediately following them from being expanded. They must be changed to forward slashes. –  Dec 07 '15 at 18:58
  • @WilliamLisowski I always forget that. Thanks for the catch! – dimitriy Dec 07 '15 at 20:29