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]

st: Re: st: A “loop” to merge dataset with a different file each time?


From   Michael Barker <[email protected]>
To   [email protected]
Subject   st: Re: st: A “loop” to merge dataset with a different file each time?
Date   Thu, 6 Feb 2014 09:28:25 -0500

I think you want a forvalues loop, to loop through the index numbers, 1-1000.
You probably want to save each result in a new data set using postfile.

Also, check your file path names. It looks like you have "uncertainty"
spelled two different ways.


insheet using "C:\ DATA\uncertainty analysis\for meging.txt", clear
sort foodcode
save "C:\DATA\uncertianity analysis\master.dta", replace

tempname results
postfile `results' (int) index (double) Thghecode using  "C:\
DATA\uncertainty analysis\myresults.dta" , replace

forvalues i=1/1000 {
     insheet using "C:\DATA\uncertianity analysis\foodpricedata_`i'.txt" , clear
     save "C:\DATA\uncertianity analysis\working.dta", replace
     sort foodcode
     merge m:1 foodcode using "C:\DATA\uncertianity analysis\master.dta",

     *** Analysis***
     generate totalghgeR = real( totalpricekg)
     gen Tghgecode =  totalghgeR *  numberofportions
     total  Tghgecode

     *** Post ***
     post `results' (`i') (e(b))

     clear
}

postclose `results'

*** View Results ***
clear
use "C:\ DATA\uncertainty analysis\myresults.dta"






On Tue, Feb 4, 2014 at 12:06 PM, Kremlin Wickramasinghe
<[email protected]> wrote:
> Dear all,
>   I want to merge two datasets and run an analysis 1000 times in a loop. I want to write a do file to automate this analysis.
>
> My master dataset has nutrition information of different foods (with a unique food code for each food name). I want to merge it with another dataset which contain food price data for those food codes. I have food prices from 1000 different stores in 1000 different Excel sheets. I have saved them with filenames with a sequence from 1-1000. (foodpricedata_1 to foodpricedata_1000) .   I want to  write a do file to automate the process of
> a)      Do m:1 merge using foodcode , but each time use a different food price data file.
> b)      Run the analysis
>
> The following do file shows the steps of running this analysis twice, first time  with the foodpricedata_1 and second time with the foodpricedata_2 files. I want to automate this to run 1000 times but each time using a unique file. I have seen tutorial on how to run a loop, but couldn’t find any resources on how to make it a loop using a different file in each time .  I would be most grateful if someone could guide me on how to do this.
>
> Do File :
>
> insheet using "C:\ DATA\uncertainty analysis\for meging.txt", clear
> sort foodcode
> save "C:\DATA\uncertianity analysis\master.dta", replace
>
> insheet using "C:\DATA\uncertianity analysis\foodpricedata_1.txt" , clear
> save "C:\DATA\uncertianity analysis\working.dta", replace
> sort foodcode
> merge m:1 foodcode using "C:\DATA\uncertianity analysis\master.dta",
>
> *** Analysis***
> generate totalghgeR = real( totalpricekg)
> gen Tghgecode =  totalghgeR *  numberofportions
> total  Tghgecode
>
>
> clear
>
> insheet using "C:\DATA\uncertianity analysis\for meging.txt", clear
> sort foodcode
> save "C:\DATA\uncertianity analysis\master.dta", replace
>
> insheet using "C:\DATA\uncertianity analysis\ foodpricedata_2.txt" , clear
> save "C:\DATA\uncertianity analysis\working.dta", replace
> sort foodcode
> merge m:1 foodcode using "C:\DATA\uncertianity analysis\master.dta",
>
> *** Analysis***
> generate totalghgeR = real( totalpricekg)
> gen Tghgecode =  totalghgeR *  numberofportions
> total  Tghgecode
>
>
> clear
>
> End of Do file …..
>
> Best wishes
> Kremlin
>
> *
> *   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