Dev,
Here is one way:
fs
local files = r(files)
foreach l of local files {
local name = substr("`l'",1,3)
insheet using "C:\Documents and Settings\Scott_2\Desktop\foo\\`l'",
clear
gen str country = "`name'"
}
Note: there are two \\ before the file name.
Hope this helps,
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Dev Vencappa
> Sent: Monday, July 18, 2005 6:53 PM
> To: [email protected]
> Subject: st: RE: reading the first X letters of a filename
>
> >>
> Statalist,
> I am running the insheet command as follows:
>
> insheet using "C:\World Bank TPD data\ARGbilateral.csv", clear
> gen str5 country=="ARG"
> ....
>
> I have about 100 other csv files similar to the above. e.g. ARMbilateral,
> BRGbilateral, CRDbilateral, etc. I have to repeat the same command on each
> of these csv files. Instead of writing a macro that would store all these
> three letter names and calling them in a loop, I was wondering if there is
> a simpler way of asking Stata to read the first three letters of the file,
> check if it is followed by the name bilateral, read in the data and
> execute the commands that follow.
>
> Apologies if this has been posted before.
>
> Many thanks
> Dev
>
>
> This message has been checked for viruses but the contents of an
> attachment
> may still contain software viruses, which could damage your computer
> system:
> you are advised to perform your own checks. Email communications with the
> University of Nottingham may be monitored as permitted by UK legislation.
>
>
> *
> * 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/
*
* 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/