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: Need help with Stata Programming
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Need help with Stata Programming
Date
Thu, 30 May 2013 20:20:16 +0100
Let me expand for the sake of anyone interested in that but finding it
too obscure.
The context is one in which we are looping any way. It doesn' t matter
how we are looping. Here we happen to be looping over filenames and
the whole pattern is just this cycle
foreach <filename> {
read data
do stuff
write results
}
(come to think of it, that's most of statistics, but never mind) PLUS
the details. So, what Michael wants is also to loop simultaneously
over years 2000, ... and it is easy to add that too.
There is an initialisation before the loop, say to
local y = 1999
Then inside the loop we could go
local y = `y' + 1
except that Stata has a shorthand for that
local ++y
which is stolen from the C programming language (which in turn ...) as
a special notation for adding 1, or bumping up as many programmers
like to say.
(At this point, a very delicate detail is that Stata does not allow
local y++
as an alternative way of adding 1. That is legal but what it does is
assign the string "++" to the macro name -y-. The explanation in turn
is that it's wired into Stata that whatever follows a legal macro name
is its new contents (no need for a space either). Too late to change
that.
Digression within digression: If you know that in C there is subtle
and important difference between ++ as prefix and ++ as suffix, that's
correct, but it is not going to be part of this story.)
That's it. Note that this extra looping is not specified by
complicating the -foreach- loop. It's just something we can do for
ourselves, given that Stata is looping anyway.
Nick
[email protected]
On 30 May 2013 19:58, Nick Cox <[email protected]> wrote:
> Nick
> [email protected]
>
> local ds1 "D:\_Dummy\multiple\1\auto_1.dta"
> local ds2 "D:\_Dummy\multiple\2\auto_2.dta"
> local ds3 "D:\_Dummy\multiple\3\auto_3.dta"
> local all ds1 ds2 ds3
> local y = 1999
> foreach x in `all'{
> use ``x'',clear
> include "D:\_Dummy\multiple\4\mpg.do"
> include "D:\_Dummy\multiple\5\sum.do"
> local ++y
> gen year = "`y' "
> save ``x''_new.dta,replace
> }
>
> local ++y
>
> could be
>
> local y = `y' + 1
>
> The whole thing could be shorter.
>
>> ************************************************
>> NOW MY NEW TASK/PROBLEM:
>> I want to create a string variable "year" which needs to be 2000 for
>> auto_1.dta, 2001 for auto_2.dta,2002 for auto_3.dta etc
>> Is could open each dataset and create it myself before starting a the loop.
>> But is there a way to create one OR more variables with different
>> values for different datasets?? within a loop
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/