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: Using foreach and forval to append data files


From   "Li Chuntao (Tony)" <[email protected]>
To   [email protected]
Subject   Re: st: Using foreach and forval to append data files
Date   Tue, 26 Mar 2013 23:28:05 +0800

My Suggestion is use macro functions to save the list of files into a
local macro

sample code as following:

clear

cd  filepath

local i: dir . file *

tokenize `"`i'"'


use `1', clear
local j=2

while `"``j''"'~="" {
append using  ``j''
 local j = `j'+1
  }


On Tue, Mar 26, 2013 at 9:01 PM, Tim Evans <[email protected]> wrote:
> Maarten,
>
> Thanks for this - very nice! Worked a treat.
>
> Best wishes
>
> Tim
>
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Maarten Buis
> Sent: 26 March 2013 12:15
> To: [email protected]
> Subject: Re: st: Using foreach and forval to append data files
>
> local k = 1
> forval i = 1/6 {
>     foreach j of numlist 1 2 8 {
>         if `k' == 1 {
>             use `i'_`j'_results
>         }
>         else {
>             append using `i'_`j'_results
>         }
>         local k = `k' + 1
>     }
> }
>
> Hope this helps,
> Maarten
>
> On Tue, Mar 26, 2013 at 1:07 PM, Tim Evans wrote:
>> 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?
>>
>> Best wishes
>>
>> Tim
>>
>>
>> _DISCLAIMER:
>> This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful.
>>
>>
>> *
>> *   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/
>
>
>
> --
> ---------------------------------
> Maarten L. Buis
> WZB
> Reichpietschufer 50
> 10785 Berlin
> Germany
>
> http://www.maartenbuis.nl
> ---------------------------------
>
> *
> *   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/
>
> _DISCLAIMER:
> This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful.
>
>
> *
> *   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