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: RE: looping up to a local macro
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: RE: looping up to a local macro
Date
Thu, 23 Jun 2011 08:40:23 +0100
Michael has not (yet) clarified the situation with his varlists, so
that we cannot decide between
optimistic take: his -letter0*- could be just -letter01- up with the *
standing for successive integers to some maximum
pessimistic take: his -letter0*- could be a mix of variable names as above
and other irrelevant variable names also starting with -letter0-.
Here's a method to segregate the two, on the latter take.
unab all : letter0*
local suffixes : subinstr local all "letter0" "", all
local count : word count `suffixes'
numlist 1/`count'
local integers `r(numlist)'
local suffixes : list integers & suffixes
Now, even on the pessimistic take, Michael could loop
foreach s of local suffixes {
rename letter0`s' letter`s'
...
}
Here is another way to get that list of suffixes
unab all : letter0*
local suffixes : subinstr local all "letter0" "", all
local OK
foreach s of local suffixes {
capture confirm number `s'
if _rc == 0 local OK `OK' `s'
}
foreach s of local OK {
...
}
On Thu, Jun 23, 2011 at 12:16 AM, Nick Cox <[email protected]> wrote:
>> It's true that there could be other variables such as
>> -letter0aardvark-, -letter0bison-, -letter0coypu-, and so forth.
>>
>> On Thu, Jun 23, 2011 at 12:13 AM, Nick Cox <[email protected]> wrote:
>>> I don't think it's clear that any explicit looping or macro
>>> manipulation is needed at all. Michael wants to change a bunch of
>>> variables -letter01- on to -letter1- on , for which a solution is
>>>
>>> renpfix letter0 letter
>>>
>>> -renpfix- has long been an official command.
>>>
>>> The -recode- part will take a varlist, so looping is also unnecessary for that.
>>>
>>> However, I can't vouch for what is needed to speed up the unspecified
>>> lines of code.
>>>
>>> Nick
>>>
>>> On Wed, Jun 22, 2011 at 11:20 PM, Sarah Edgington <[email protected]> wrote:
>>>> Michael,
>>>> What do you mean by "I can't quite get it to work"? What problem are you
>>>> having?
>>>> Without more information about what's going wrong it is really difficult to
>>>> offer much assistance. The form of your loop looks fine. Maybe there's
>>>> some problem with the code in the loop itself. Does it work if you specify
>>>> a number instead of the local macro? You're setting the value of letters in
>>>> the same dofile as the forvalues loop, right? If you aren't that's going to
>>>> be a problem.
>>>
>>> Michael Costello
>>>
>>>> I'm writing to ask about combining loops with local values. I have a local
>>>> macro that I need to set equal to some value, different for each database.
>>>> Let's say, for example, 50 in one db, 100 in another. Then I want to
>>>> preform some loop operation on all the variables from
>>>> letter1 up to letter50 or letter100 (depending on the database). Is that
>>>> possible, with some change to the code below? I can't quite get it to work.
>>>>
>>>> local letters 100
>>>>
>>>> forvalues i = 1(1)`letters' {
>>>> quietly: rename letter0`i' letter`i'
>>>> quietly: recode letter`i' (9=.) (2=1) (1=0)
>>>> * more lines of code here
>>>> }
>>>>
>>>> Basically, instead of copy ~400 lines of code into several dozen cleaning
>>>> files, I'd like to just set the maximum value (50, 100, 20,
>>>> etc) for the database and have it run one cleaning file. Thanks for any
>>>> help you can provide!
*
* 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/