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: RE: looping through data files in do-file

From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: looping through data files in do-file
Date   Thu, 21 Oct 2010 16:14:35 +0100

1. Too many "". You can't usually concatenate strings by "frog""toad""newt". (-display- lets you get away with something that looks like that, but that's illusion. It is treating them separately, and not concatenating them.)

2. Dangerous to use \. Use / even through this looks like Windows. 

3. I wouldn't use globals so much, but that's largely a style preference. 

version 9
local year "_2009"
local filename "CPT_"
local suffix ".dta"
local filepath "i:/My_Docs_on_I/BEHRdata/CPT_2009/"
local mod  "_R13"

foreach month in "02"  {
	di "`filepath'CPT_`month'_2009.dta"
 	use `filepath'CPT_`month'_2009.dta,clear

That's not a real loop yet. 

[email protected] 

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Broudy, David, DOH
Sent: 21 October 2010 15:55
To: [email protected]
Subject: st: looping through data files in do-file

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:

*   For searches and help try:

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