Bookmark and Share

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: Looping gen sequentially over datasets


From   Steve Nakoneshny <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Looping gen sequentially over datasets
Date   Wed, 25 Sep 2013 11:02:49 -0600

Alex,

It isn't clear whether you're going to need to -merge- or -append-, but I assume the latter. How about something like:

use "dataset1"
g id=1
lab def idlab foo
lab val id idlab
save, replace

forval i = 2/40 {
	append using "dataset`i'"
	replace id=`i' if (missing)id
}

save "newdata", replace

Steve

On 2013-09-25, at 10:26 AM, Alex Sutherland wrote:

> Dear Statalisters,
> 
> I am trying to generate sequential ID variables over a series of datasets.
> There are 40 datasets representing 40 schools and I want to generate an ID
> variable for each school (ranging from 1-40) without the need to open and
> edit each file manually. (I'm likely to have to repeat this process later
> and I want to combine the datasets once the ID variable is created, hence
> the need for unique identifiers for each school). I keep thinking it's going
> to involve a foreach loop over the datasets combined with another loop
> (forval?), but then I'm up against the limits of my Stata-ing so any help
> gratefully received.
> 
> Thanks in advance,
> 
> Alex
> 
> Dr. Alex Sutherland
> Research Methods Associate,
> Social Sciences' Research Methods Centre (SSRMC)
> http://www.ssrmc.group.cam.ac.uk/
> 
> Office 3.12
> Institute of Criminology,
> University of Cambridge,
> Sidgwick Site
> CB3 9DA
> Tel: +44 (0)1223 746519
> [email protected] 
> http://www.crim.cam.ac.uk/people/academic_research/alex_sutherland/
> 
> *
> *   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/
> 



*
*   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/


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