Bookmark and Share

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


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

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


From   Sarah Elizabeth Edgington <[email protected]>
To   [email protected], <[email protected]>
Subject   Re: st: looping through data files in do-file
Date   Thu, 21 Oct 2010 08:15:10 -0700

David,
You're sticking in extra quotation marks you don't need. In the display command it doesn't matter because display allows multiple quoted sections with other syntax in between but it does matter with the actual path because Stata is trying to parse all of those quotation marks as part of your filename.
Instead of
use " "$filepath""CPT_""`month'""_2009"".dta" ",clear
try

use "${filepath}$(filename}`month'`year'${suffix} " ,clear
or
use " "${filepath}CPT_`month'_2009.dta ",clear
if you don't want to use all the macros you've set up.
The braces aren't strictly necessary in the first example but I find they make it a lot easier for me to parse global macros and they avoid problems when a global macro is followed immediately by some non-macro text.

-Sarah

 At 07:55 AM 10/21/2010, Broudy, David, DOH wrote:
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index