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: Need help with Programming/ running a forvalues loop inside foreach loop


From   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Need help with Programming/ running a forvalues loop inside foreach loop
Date   Tue, 5 Nov 2013 16:00:36 -0500

On Tue, Nov 5, 2013 at 3:42 PM, Michael Stewart
<[email protected]> wrote:
> Dear Statalist member,
>
> I am writing to seek help with stata programming and I am dealing with
> rather large datasets and am resorting to loading relevant variables
> of the dataset for analysis secondary to memory constraint.
>
> I especialy need help running a  forvalues loop inside a foreach loop .
>
> Details are as follows :
> *************************
> My data is in a folder "Z:data"

Note that "Z:data" is different from "Z:\data". The latter is more likely.

>
> My files are named dataset_1 to dataset_10 (denoting years 2001 till
> 2010) and each file is in the order of  gigabytes with 40 variables
> each .
>
> For my analysis I have to use (either var1 OR var2 OR var3) along with
> (var5+var6+var7) from each dataset.
>
> As it is a multiyear analysis, I need to combine (either var1 OR var2
> OR var3) along with (var5+var6+var7) from each dataset(dataset_1 to
> dataset_10) and analyze.
>
>
> My  dofiles is as follows
>
> *******************************
> clear
> macro drop _all

clear _all is much more common in the newer versions of Stata

> global data "Z:data"
"Z:\data"???

>
> foreach outcome in  var1 var2 var3 {
// if you don't clear the memory inside the loop, you will accumulate
all the vars from all the loop iterations
clear
local interesting "`outcome' var5 var6 var7"

> use `interesting' "$data\dataset_1",clear
> forvalues i=2/10{
> append using "$data\dataset_`i'",keep(`interesting')
>
> }
> svyset [pweight= var5],strata  (var6 ) psu ( var7 )
> svy:mean `outcome'
>
> }
> *************************************************************
>
> QUESTION:  How can I make the forvalues loop  merge only relevant
> variables from each dataset into one big dataset  before proceeding to
> the next step so that I will have multi year dataset for analysis at
> subsequent steps
>
> To further clarify the question:
>
> After the foreach loop  takes var1 + var 5-7 from dataset_1; I would
> like the forvalues loop to take var1 + var 5-7 from dataset_2/10 ,
> merge them and procedd further statistical analysis.
>
> In next cycle, the foreach loop will take var2 + var 5-7 from
> dataset_1; I would like the forvalues loop to take var2 + var 5-7 from
> dataset_2/10 , merge them and procedd further statistical analysis.
>
> But with my loop, the forvalues loop is not merging multipe
> files(dataset_2 to dataset_10) before proceeding for furtehr analysis.
>
> Any suggestions/recommendations are welcome
>
> Thank you very much for your time.
>
> --
> Thank you ,
> Yours Sincerely,
> Mike.

Best, Sergiy

> *
> *   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