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 at the end of May, and its replacement, statalist.org is already up and running.


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

st: Foreach loops for each variable.


From   natasha agarwal <agarwana2@googlemail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: Foreach loops for each variable.
Date   Thu, 22 Nov 2012 19:42:05 +0000

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index