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]

Re: st: multiple merging

From   Abhimanyu Arora <>
Subject   Re: st: multiple merging
Date   Tue, 3 Apr 2012 12:26:02 +0200

Hi Nick
Thanks very much indeed. Helps clear my understanding both of
-gettoken- and the use of compound double quotes.
Even as I await Ryan Knight's reply with regard to SSC's -mergeall-, I
was trying using Mike M Mitchell's -nmerge- (the -which- command
doesn't seem to display the version of the do file).

The use of nmerge requires the file names in its syntax (simply
separated by spaces, hence the reason for using the extended macro fcn
clean—but error both with and without this)

After the use of gettoken I get the file names but each in double
quotes. Say the macro files contains the strings "file1.dta"
"file2.dta" ...

Assume we start with file0 (the one to which others will merge)

Complementing with
local files4=subinstr(`"`files'"',".dta","",.)

I get

. nmerge date using `"`files4'"'
One or more filenames after 'using' required
(no error though)

di `"`files4'"'

"file1" "file2"....and so on


local files2 : list clean files
local files3=subinstr(`"`files2'"',".dta","",.)

I get
. nmerge date using `"`files3'"'
'file2' found where nothing expected

di `"`files3'"'

file1 file2... and so on

Not able to understand why this error. I thought the second option
should work out but don't know what's blocking.


On Tue, Apr 3, 2012 at 9:48 AM, Nick Cox <> wrote:
> Your use of -gettoken- is quite illegal. -gettoken- is perhaps most
> often used in this form
>  gettoken emname1 emname2 : emname3
> in which the -emname*- are extended macronames. Focus on the "names":
> you are feeding -gettoken- with macro contents as the macro
> _reference_ `r(files)' will get expanded by Stata before -gettoken-
> sees it.
> Nor will r(files) be acceptable to -gettoken-.
> This is an example of will work:
> fs <whatever>
> local files `" `r(files)' "'
> gettoken first files : files
> In your case the -local- command may be enough. There is no need to
> clean up the " " as they are there for a purpose, to bind names with
> spaces.
> -fs- is from SSC.
> Nick
> On Mon, Apr 2, 2012 at 11:22 PM, Abhimanyu Arora
> <> wrote:
>> I have datasets which I need to merge into a single file. All files
>> have a (string) variable that uniquely identifies observations in the
>> files.
>> There are two user-written commands I found in the SSC—mergeall (Ryan
>> Knight) and nmerge(Michael N. Mitchell).
>> I am facing problems with both.
>> In the first case I get a type mismatch error even though I specify
>> the string option with force
>> In the second case I am unable to get my file names (except the first
>> file) in a macro.  I used fs(Nick Cox) get the file names in r(files).
>> Next I type in  gettoken first `r(files)' : `r(files)'
>> That shows the following error
>> _"filename1.dta invalid name
>> I tried using extended macro function to clean up the quotes but to no
>> avail.
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

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