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: Calculating variable-averages of time-spans (laid out case by case via variables)


From   Robert Picard <[email protected]>
To   [email protected]
Subject   Re: st: Calculating variable-averages of time-spans (laid out case by case via variables)
Date   Fri, 11 Mar 2011 14:03:40 +0100

You need to learn how to group records in Stata. Here one way of doing it:

sort idcode year
isid idcode year

by idcode: gen m = (wks_work + wks_work[_n-1]) / 2

* Tag the burnout year
gen target = year == burnoutyear

* Push the burnout year down to the last record of the idcode group
sort idcode target year
by idcode: gen mm = m[_N]


On Fri, Mar 11, 2011 at 1:32 PM, Wolfgang Feudenheim
<[email protected]> wrote:
> Hi,
>
> I am currently working on an analysis of economic data in OECD-countries. For each country, I separately fixed a key-year. For this specific year and the two preceding years I want to read out averages of economic indicators such as "GDP/capita" etc.
>
> In the following, I try to illustrate my problems with the help of the example dataset
> "National Longitudinal Survey.  Young Women 14-26 years of age in 1968" by pretending I was interested in the average workload before the occurance of a burnout (sorry, couldn't make up any more positive scenario ...). Unfortunately, the time data is not available on a year-by-year-basis but in irregular steps. Therefore, I just observe one specific year and its preceding year. I am running my analysis on
>
> -Stata/IC 11.1 for Mac (64-bit Intel)
> -Born 04 Nov 2010
>
> Here is the code:
>
> -use http://www.stata-press.com/data/r11/nlswork.dta, clear
> -*Add Burnout-Values to Dataset*
> -gen burnoutyear=.
> -replace burnoutyear=73 if idcode==1
> -replace burnoutyear=72 if idcode==2
>
> -*Generate Variable for all observations of one person (idcode) that presents the average of weeks worked in burnout-year and*
> -*burnout-preceding year*
> -egen avworkload_b=mean(wks_work[_n]+wks_work[_n-1]) if (year==burnoutyear)&(idcode[_n]==idcode[_n-1])
>
>
> The problems that occur are the following:
>
> 1. For both, "idcode==1" and "idcode==2", the wrong result, namely "25" is displayed. The average values should however be "27 and "17.5".
>
> 2. The variable "avworkload_b" is only inserted into the dataset for the year indicated by "burnoutyear" for the respective "idcode". I want to have this value displayed for all years of each "idcode".
>
> Thanks in advance!
>
> Wolfgang
> *
> *   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/
>

*
*   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