Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: looping through data files in do-file

From   "Broudy, David, DOH" <[email protected]>
To   <[email protected]>
Subject   st: looping through data files in do-file
Date   Thu, 21 Oct 2010 08:55:05 -0600

I'm trying to create a do file that will open a monthly data file, run a
couple of commands to create a variable, select and keep records, save a
new file and go to the next month.  Later these monthly abstract files
will be merged into an annual file.

 I'm having trouble building the string with the path and file names.
Each month's data differs only in a two character number that represents
the calendar month  01 is Jan etc

The new file will be named the same as the input file but with _R13

version 9

local year "_2009"

global filename "CPT_"

global suffix ".dta"

global filepath "i:\My_Docs_on_I\BEHRdata\CPT_2009\"

global mod  "_R13"

foreach month in "02"  {

display "$filepath""CPT_""`month'""_2009"".dta"

use " "$filepath""CPT_""`month'""_2009"".dta" ",clear


This seems to work and displays a correct path and filename.

But when I put the same string in a use statement it gives an error.
I've tried a lot of combinations of single and double quotes-see run

 . do "C:\DOCUME~1\DAVID~1.BRO\LOCALS~1\Temp\STD05000000.tmp"

. version 9

 . local year "_2009"

 . global filename "CPT_"

 . global suffix ".dta"
. global filepath "i:\My_Docs_on_I\BEHRdata\CPT_2009\"
. global mod  "_R13"

. foreach month in "02"  {

  2. display "$filepath""CPT_""`month'""_2009"".dta"

  3. use " "$filepath""CPT_""`month'""_2009"".dta" ",clear


. }

i:\My_Docs_on_I\BEHRdata\CPT_2009\CPT_02_2009.dta   [this is the string

invalid 'i'    [this apparently is an error trying to execute use]

Doesn't seem like running an analysis program against a set of recurring
data files should be so complicated.  What am I missing?

Thank you in advance


David Broudy 
NM State Dept Health

Confidentiality Notice: This e-mail, including all attachments is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited unless specifically provided under the New Mexico Inspection of Public Records Act. If you are not the intended recipient, please contact the sender and destroy all copies of this message. -- This email has been scanned by the Sybari - Antigen Email System. 

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index