Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: RE: looping through data files in do-file


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
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. 

Nick 
n.j.cox@durham.ac.uk 


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Broudy, David, DOH
Sent: 21 October 2010 15:55
To: statalist@hsphsun2.harvard.edu
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
inserted.

 
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
below.

 . 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
displayed]

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 
Epidemiologist,
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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index