Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Foreach loops for each variable.


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
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 <agarwana2@googlemail.com> 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index