Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Looping and replacing embedded text


From   Neil Shephard <nshephard@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Looping and replacing embedded text
Date   Tue, 8 Nov 2005 21:06:33 +0000

On 11/8/05, Joe Crespo <joe.crespo@hotmail.com> wrote:
> This is a question about running a loop.
>
> I run identical opperations on several data files containing the same
> variables but representing different geographical markets i.e., - US states
> or census regions. As I go through each region the only text in the program
> that changes is the text that identifies that region.
>
> My question is whether I can replace a serious of commands that are
> identical except for a census abreviation which is embedded somewhere in the
> program, with a single command and a  loop that replaces the census
> abreviation anywhere it is found in the program.
>
> For example, can I replace the following lines of commands with a single
> line and a loop?
> _____________________________
>
> do d:\do\new_reg.txt
> do d:\do\enc_reg.txt
> do d:\do\pac_reg.txt
> do d:\do\mac_reg.txt
> _________________________
>
> THanks for any advice. Also if anybody can point to a manual that provides
> advice on the relevant programming language I'd be much obliged.
>

The above could be simplified to...

foreach state in new enc pac mac{
   do d:\do\`state'_reg.txt
}

The relevant section in the manual for this particular command is [P]
foreach or [P] forval.  On-line help "foreach" of these can be
obtained by typing -man foreach- (and you may find -man forval- of
interest as well).  I'm afraid I don't have Stata 9 (I assume) manuals
to hand at present so could not say if these commands are documented
elsewhere, perhaps in [U] as they are in the Stata 8 manuals (if I
remember correctly since I don't have them to hand either!).

One thing to note is that it is convention (but by no means essential,
as you have found) to give text files which contain a series of Stata
commands the extension .do (as in a Stata do-file).  The application
that files carrying such an extentions will be associated with depends
upon your operating system and configuration.

HTH's

Neil

*
*   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/



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