Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Appending several files

From   "Nuno Soares" <>
To   <>
Subject   RE: st: Appending several files
Date   Mon, 16 Oct 2006 08:26:00 +0100

Thanks David. 

-----Original Message-----
[] On Behalf Of
Sent: 16 October 2006 03:18
Subject: Re: st: Appending several files

Ulrich Kohler already supplied an interesting solution.  I'd like to show
how I usually handle this sort of situation (which does come up often --
with either -append- or -merge-). This might be more of an old-fashioned
programmer's method.

local j "0"
foreach f in `r(files)' {
 if `j' == 0 {
  use `f'
 else {
  append using `f' 
local ++j

One can be more clever by putting the ++j (or j++) inside the -if-
statement, if you prefer.
I haven't yet tried that technique; it might go if `j++' ==0 (if I have the
syntax right -- or is it `=j++' ... ?) or even if ~`j++'

I hope this is may be useful.
-------------- Original message from "Nuno Soares" <>:

> Hi everyone,
> I'm trying to make Stata to append several files that are in a given 
> directory without the need of specifying the name of each one. I was 
> thinking of doing something like this (using the fs package):
> clear
> cd "C:\data" 
> qui fs *
> foreach f in `r(files)' {
> append using `f' 
> }
> The problem with this code is that Stata doesn't allow to append a 
> data file when it doesn't have a file opened (it will return the error
> "no dataset in use
> r(3)")
> This means that I must have at least one of the files loaded in Stata 
> before using the above code. But if I then use the above code, I would 
> end up getting duplicate observations, because the file I've first 
> used is also appended... Any ideas of how to solve this issue?
> Best,
> Nuno
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index