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

st: RE: Looping and replacing embedded text


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Looping and replacing embedded text
Date   Tue, 8 Nov 2005 21:06:59 -0000

First off, it seems you are using Windows. 
Windows, following DOS, uses the backslash \ 
as a separator here, as you know, but that was 
a bad idea on the part of Microsoft,
given its common use elsewhere in computing 
as an escape character. 

As you are sensing that your Stata code can be 
made more compact, a price for doing that 
more easily is to use / instead. Stata will
understand what you mean and translate for 
the sake of your operating system. This isn't 
compulsory, but it will make your life
easier. A section somewhere in [U] explains
how the backslash can bite (a mixed metaphor, 
but let it stand). 

In your case you can loop like this. Your 
code, forward-looking, is 

do d:/do/new_reg.txt
do d:/do/enc_reg.txt
do d:/do/pac_reg.txt
do d:/do/mac_reg.txt

and this could become 

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

which is not much of a saving here, 
but there will be less trivial examples
in your files. 
	
The programming language here is
naturally Stata. [P] is the manual, 
but -foreach- and -forvalues- have
been discussed in the Stata Journal
and elsewhere. A 

. search foreach 

may suggest that only one person 
is much interested in this territory, 
but that's an illusion. 

[P]     foreach . . . . . . . . . . . . . . . . . . . . . . .  Loop over items
        (help foreach)

FAQ     . . . . . . . . .  Counting distinct strings across a set of variables
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        7/04    How do I count the number of distinct strings
                across a set of variables?
                http://www.stata.com/support/faqs/data/distinctstrings.html

FAQ     . . . . . . . . . . Making foreach go through all values of a variable
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        4/03    Is there a way to tell Stata to try all values of a
                particular variable in a foreach statement without
                specifying them?
                http://www.stata.com/support/faqs/data/foreach.html

FAQ     . . . . . . . . . . . . . . Getting nice time of day labels on a graph
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        9/03    How can I get "nice" time of day labels on a graph?
                http://www.stata.com/support/faqs/graphics/nicetime.html

Example . . . . Stata learning module:  Working across variables using foreach
        . . . . . . . . . . . . . . . . . .  UCLA Academic Technology Services
        8/03    http://www.ats.ucla.edu/stat/stata/modules/acrossvars.htm

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros

SJ-3-1  pr0008   Speaking Stata: On structure & shape: the case of mult. resp.
        . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox & U. Kohler
        Q1/03   SJ 3(1):81--99                                   (no commands)
        discussion of data manipulations for multiple response data

SJ-2-2  pr0005  . . . . . .  Speaking Stata:  How to face lists with fortitude
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/02   SJ 2(2):202--222                                 (no commands)
        demonstrates the usefulness of for, foreach, forvalues, and
        local macros for interactive (non programming) tasks

SJ-1-1  pr0003  . . . Speaking Stata: How to repeat yourself without going mad
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/01   SJ 1(1):86--97                                   (no commands)
        describes systematic ways of repeating the same or similar
        operations

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

Joe Crespo
 
> 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.

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