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: Updating IDs in a foreach loop
From
Sergiy Radyakin <[email protected]>
To
[email protected]
Subject
Re: st: Updating IDs in a foreach loop
Date
Fri, 5 Nov 2010 09:05:55 -0400
On Fri, Nov 5, 2010 at 8:53 AM, Florian Seliger <[email protected]> wrote:
> Dear Statalist,
>
> 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.
initialize a counter before the loop
increment the counter in the loop
> 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:
>
local i=0
> foreach file in "acer.dta" "adobe.dta" "affiliated.dta" ... {
local i=`i'+1
> use "`file'"
> keep id sic_* sic
>
> ....(a lot of other commands)...
>
> rename id id_1
rename id id_`i'
> label var id_1 "`file'"
> save "sic_cited_`file'", replace
> }
>
>
> Thank you in advance,
> Florian
*
* 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/