There is an extra sting in the problem. The first nine months
are represented by files with month elements 01 ... 09.
But you are supplying 1 ... 9. The leading "0" needs to be supplied.
Here is one way to do it:
forval y = 1999/2002 {
forval m = 1/12 {
local M : di %02.0f `m'
infile using mydict.dct, using(myfile_`y'_`M'.txt) clear
save "$root\Data\myfile_`y'_`M'.dta", replace
}
}
I also used -forval- here for conciseness and made some other
small changes, mostly cosmetic.
Nick
[email protected]
Oleksandr Talavera, EUV
> maybe I misunderstood the problem... If all data files have the same
> structure, then
> in the loop just specify the data file that you want to use...
> *****************
> foreach year in 1999(1)2002 {;
> foreach month in 1(1)12 {;
> infile using mydict.dct, using myfile_`year'_`month'.txt clear;
> save "$root\Data\myfile_`year'_`month'.dta", replace;
> };
> };
> ******************
> and mydict.dct maybe jus like
>
> ********
> dictionary {
> int a
> int b
>
> }
Sascha O. Becker
> > I have a series of ASCII files that have names carrying the year and
> > month as part of their names:
> >
> > myfile_1999_01.txt
> > myfile_1999_02.txt
> > ...
> > myfile_2002_12.txt
> >
> > and want to read them in using one and the same dictionary.
> >
> > I have a do-file (mydata.do) in which I loop year and month
> >
> > ***
> > foreach year in 1999(1)2002 {;
> > foreach month in 1(1)12 {;
> >
> > infile using mydict.dct, clear;
> > save "$root\Data\myfile_`year'_`month'.dta", replace;
> >
> > };
> > };
> > ***
> >
> > In order to call the correct ASCII files, I need to adress
> them in my
> > *.dct file (mydata.dct) in the first line.
> >
> > +++++
> >
> > Using locals does not work:
> >
> > dictionary using "myfile`year'_`month'.txt" {
> > STUFF
> > }
> >
> > because Stata does not carry them over to the *.dct file.
> >
> > +++++
> >
> > So, I thought, I'd use macros and add the following line to
> the above
> loop:
> >
> > global year "`year'"
> > global month "`month'"
> >
> > and then call them in my *.dct file
> >
> > 1) dictionary using "myfile_$year_$month.txt"
> >
> > or
> >
> > 2) dictionary using "myfile_{$year}_{$month}.txt" {
> >
> > but this does not work either.
> >
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/