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, 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 <>
To   "" <>
Subject   Re: st: Foreach loops for each variable.
Date   Fri, 23 Nov 2012 07:53:22 +0000

-separate- is dedicated to your immediate problem.


On 22 Nov 2012, at 19:42, natasha agarwal <> 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.

*   For searches and help try:
*   For searches and help try:

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