Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: RE: Using foreach and forval to append data files


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Using foreach and forval to append data files
Date   Tue, 26 Mar 2013 12:32:21 +0000

As a minute refinement, note that

foreach j in 1 2 8 {

is perfectly acceptable to Stata.

Nick

On Tue, Mar 26, 2013 at 12:18 PM, Barclay Matthew (SHEFFIELD TEACHING
HOSPITALS NHS FOUNDATION TRUST) <matthewbarclay@nhs.net> wrote:

> You can append to an empty dataset, so
>
> clear
> forval i = 1/6 {
> foreach j of numlist 1 2 8 {
> append using `i'_`j'_results
> }
> }
>
> will do what you want.

statalist@hsphsun2.harvard.edu] On Behalf Of Tim Evans

> Using Stata 11.2, I've created 18 individual results sets that have the following naming schedule:
>
> 1_1_results.dta
> 1_2_results.dta
> 1_8_results.dta
> 2_1_results.dta
> 2_2_results.dta
> 2_8_results.dta
> 3_1_results.dta
> 3_2_results.dta
> 3_8_results.dta
>
> This continues until the first number reaches 6.
>
> What I am trying to achieve is an automated process of appending all data files and I was using the following code - but this adds a duplication of the file 1_1_results:
>
> use 1_1_results.dta, replace
> forval i = 1/6 {
> foreach j of numlist 1 2 8 {
> append using `i'_`j'_results
> }
> }
>
> I then tried this:
>
> use 1_1_results.dta, replace
> forval i = 2/6 {
> foreach j of numlist 1 2 8 {
> append using `i'_`j'_results
> }
> }
>
> Which I then realised that I miss out on any file named:
>
> 1_2_***
> 1_8_***
>
> Other than using the first loop and using -duplicates- to identify and then remove the duplicates, is there a more direct way of using and then appending datasets?
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index