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: Foreach loops for each variable.


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Foreach loops for each variable.
Date   Fri, 23 Nov 2012 07:53:22 +0000

-separate- is dedicated to your immediate problem.

Nick

On 22 Nov 2012, at 19:42, natasha agarwal <[email protected]> wrote:

Hello Stata users,

I have a understanding failure here in how the foreach loop is
working. I have a dataset with 40 countries spanning from 1980-2004.

Now for each country I have to create a variable which is going to
look like follows:

gen agri_1980 = Dst1980 if pa=="Agriculture, Hunting and Forestry" & year==1980
gen mini_1980 = Dst1980 if pa=="Mining and Quarrying" & year==1980
gen basic_1980 = Dst1980 if pa=="Basic Metals and Fabricated Metal" & year==1980
gen construction_1980 = Dst1980 if pa=="Construction" & year==1980
gen electrical_1980 = Dst1980 if pa=="Electrical and Optical
Equipment" & year==1980
gen electricity_1980= Dst1980 if pa=="Electricity, Gas and Water
Supply" & year==1980
gen financial_1980 = Dst1980 if pa=="Financial Intermediation" & year==1980 gen food_1980 = Dst1980 if pa=="Food, Beverages and Tobacco" & year==1980
gen hotels_1980 = Dst1980 if pa=="Hotels and Restaurant" & year==1980
gen mach_1980 = Dst1980 if pa=="Machinery, nec" & year==1980

Basically here I am just creating variables that takes the values of
Dst for a particular industry in 1980. I have to do the same for all
the years and then over 40 countries. I tried doing it following
Nick's advice on how to use foreach for each values in the variable,
but failed.

Is there anybody that could help me with the same, I would be grateful.

Thanks,
Natasha
*
*   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