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: Populating variables based upon integer value in the observation


From   Matthew White <[email protected]>
To   [email protected]
Subject   Re: st: Populating variables based upon integer value in the observation
Date   Tue, 24 Jul 2012 11:28:52 -0400

Hello,

I think you just need to tweak your code. Replace the -if- command
with the -if- qualifier. From:

forvalues x = 1/10 {
gen yr`x' = 0
                if `x'<= a {
                        replace yr`x' = b
                }
}

To:

forvalues x = 1/10 {
    gen yr`x' = 0
    replace yr`x' = b if `x' <= a
}

For more on the difference between the -if- command and -if- qualifier
and why the -if- command took -a- to mean -a[1]-, see:
<http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier>.

Another option is -cond()-:

forvalues x = 1/10 {
    gen yr`x' = cond(`x' <= a, b, 0)
}

Best,
Matt

On Tue, Jul 24, 2012 at 6:24 AM, Mathew Wilkins <[email protected]> wrote:
>
> 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



-- 
Matthew White
Project Associate
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index