Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
Attachment:
pic02220.gif
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Populating variables based upon integer value in the observation
From
Mathew Wilkins <[email protected]>
To
statalist <[email protected]>
Subject
st: Populating variables based upon integer value in the observation
Date
Tue, 24 Jul 2012 12:24:43 +0200
Dear Stata listers,
I am wondering if it is possible to populate a certain number of variables
based upon san integer variable in the observation. I am still relatively
new to stata and would relatively easily be able to program this in Access
or Excel.
I have variables a, b and yr1-yr10 (all integer)
I would like some commands that will populate variable b into variables
yr1-yr10, the number of the variables yr1-yr10 that are populated is based
upon variable a. The below example dataset show an example of what I would
like to populate
obs | a | b | yr | yr2 | yr | yr4 | yr5 | yr6 | yr7 | yr8 | yr | yr1
| | | 1 | | 3 | | | | | | 9 | 0
------+-----+------+-----+------+-----+-------+------+------+------+------+-----+------
1 | 4 | 25 | 25 | 25 | 25 | 25 | 0 | 0 | 0 | 0 | 0 | 0
------+-----+------+-----+------+-----+-------+------+------+------+------+-----+------
2 | 10 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3
------+-----+------+-----+------+-----+-------+------+------+------+------+-----+------
3 | 7 | 19 | 19 | 19 | 19 | 19 | 19 | 19 | 19 | 0 | 0 | 0
------+-----+------+-----+------+-----+-------+------+------+------+------+-----+------
4 | 1 | 14 | 14 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
------+-----+------+-----+------+-----+-------+------+------+------+------+-----+------
5 | 9 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 5 | 0
------+-----+------+-----+------+-----+-------+------+------+------+------+-----+------
6 | 8 | 79 | 79 | 79 | 79 | 79 | 79 | 79 | 79 | 79 | 0 | 0
I have managed to use a forvalues loop to create the variables yr1-yr10 and
another loop to then populate them.
forvalues x = 1/10 {
gen yr`x' = 0
if `x'<= a {
replace yr`x' = b
}
}
However the yr columns that are being populated are only based on variable
a in the first observation (result set below).
a b yr1 yr2 yr3 yr4 yr5 yr6 yr7 yr8 yr9 yr10
4 25 25 25 25 25 0 0 0 0 0 0
10 3 3 3 3 3 0 0 0 0 0 0
7 19 19 19 19 19 0 0 0 0 0 0
1 14 14 14 14 14 0 0 0 0 0 0
9 5 5 5 5 5 0 0 0 0 0 0
8 79 79 79 79 79 0 0 0 0 0 0
I have tried to use an additional forvalues loop for the observation
however this either does not work or take too long to process (the full
scale dataset I am working on is approximately 11,000 observations and the
yr columns go from 1 – 30.
Many thanks in advance for your time and consideration.
Regards
Mathew Wilkins
United Nations Climate Change Secretariat (UNFCCC)
Mathew Wilkins
Statistical Assistant
Strategy & Policy Development
(Embedded image moved to file: pic02220.gif)
United Nations
Climate Change Secretariat
Martin-Luther-King Strasse 8
53175 Bonn, Germany
Phone +49 228 815 1864
Fax +49 228 815 1999
[email protected]
unfccc.int
Description: GIF image