Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Updating IDs in a foreach loop

From   "Florian Seliger" <>
Subject   st: Updating IDs in a foreach loop
Date   Fri, 05 Nov 2010 13:53:09 +0100

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.
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".


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

Thank you in advance,

GRATIS! Movie-FLAT mit über 300 Videos. 
Jetzt freischalten unter
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index