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]
st: RE: Updating IDs in a foreach loop
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: Updating IDs in a foreach loop
Date
Fri, 5 Nov 2010 13:06:01 +0000
To loop simultaneously over lists of words and the integers 1 up, you just do
this
local i = 1
foreach file in <list> {
<code referring to `i'>
<code referring to `file'>
local ++i
}
So, each time around the loop you "bump up" `i' (jargon for "adding 1").
As you say, within your loop you can only do this:
gen id_`file' = <whatever>
as long as `file' is consistent with variable name rules. (So punctuation other than underscores wouldn't work, etc.)
Actually, I disagree with you. Unique identifier names just make file manipulation more difficult. What usually would work better is
gen id = `"`file'"'
gen id = `i'
There is a tutorial in
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
which spells out the first part of my reply. (Ignore the stuff on -for- as ancient history, and know that -levels- still exists, but StataCorp prefer that you use -levelsof-.)
Nick
[email protected]
Florian Seliger (edited)
I have specified a -foreach- loop for about 250 company files.
Each file has an ID variable with values between 1-100,000.
At the end of the loop, I want to ask Stata to create an ID that uniquely identifies each dataset.
I have created a unique label for each ID variable, but I also want to give them a number or a company-specific name.
Please take a look at the example below. At the end, "sic_cited_acer_consolidated2.dta" should have an ID variable named "id_1",
"sic_cited_adobe_consolidated2.dta" should have an ID variable "id_2",
and so on. The problem is how to update the number of id_1 after the first file has been accomplished.
An even better alternative would be to have an ID variable "id_acer", "id_adobe", and so on, but renaming with a name that includes the filename `file' yields an "invalid name".
Example:
foreach file in "acer.dta" "adobe.dta" "affiliated.dta" ... {
use "`file'"
keep id sic_* sic
....(a lot of other commands)...
rename id id_1
label var id_1 "`file'"
save "sic_cited_`file'", replace
}
*
* 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/