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

From |
Linn Renée Naper <linn.naper@ecgroup.no> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: RE: Create variable conditional on several others |

Date |
Fri, 24 Apr 2009 09:59:19 +0200 |

Hi Nick Thanks, these codes are really useful and works just perfect. Great! Linn -----Opprinnelig melding----- Fra: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] På vegne av Nick Cox Sendt: 23. april 2009 18:19 Til: statalist@hsphsun2.harvard.edu Emne: st: RE: Create variable conditional on several others You presumably need also to factor in wrapping around the end of the year. For example, on 5 November there will be prices for DEC, JAN, FEB. Note that the names JAN ... DEC just make programming more difficult, so you could (arguably should) -rename-. This is easy through renvars JAN-DEC \ f1-f12 where -renvars- can be found through -search-. There are two situations: the non-missing months are consecutive 1 2 3 for month 12 2 3 4 1 ... 10 11 12 9 Or not 1 11 12 (really 11 12 1) for month 10 1 2 12 (really 12 1 2) 11 The current month is gen curmon = month(mydate) Here is the code done slowly by going through three cases: forval j = 1/3 { gen month`j' = . } forval m = 1/12 { local M1 = cond(`m' == 12, 1, `m' + 1) local M2 = cond(`m' == 11, 1, cond(`m' == 12, 2, `m' + 2)) local M3 = /// cond(`m' == 10, 1, cond(`m' == 11, 2, cond(`m' == 12, 3, `m' + 3))) replace month1 = f`M1' if curmon == `m' replace month2 = f`M2' if curmon == `m' replace month3 = f`M3' if curmon == `m' } The code could be shorter, but it might then be (even) more cryptic. Nick n.j.cox@durham.ac.uk Linn Renée Naper I have the following challenge related to creating new variables. In my data I have daily settlement prices from a futures market. For each date (day) I have the variables JAN, FEB---- DEC (12 months). Every day there are registered prices for the next 3 months, meaning that for a given date 27dec2007 I may have observations for JAN FEB and MAR, and missing in all other months. Under is a part of these data: mydate JAN FEB MAR 03dec2007 7.6 5.6 04dec2007 7.75 7 05dec2007 8 7.63 06dec2007 9 7.65 07dec2007 8 7.75 10dec2007 8.7 8.7 11dec2007 8.45 8.45 12dec2007 7.43 7.43 13dec2007 7.45 7.45 14dec2007 6.3 6.2 17dec2007 6.25 6.25 18dec2007 6.93 6.93 19dec2007 7.2 7.2 20dec2007 6.25 7.13 21dec2007 7 7.45 27dec2007 6.7 6.5 6 28dec2007 6.63 6.75 6 02jan2008 6.73 6 03jan2008 6.63 5.78 04jan2008 6.38 5.73 07jan2008 6.45 6.23 08jan2008 6.3 6 09jan2008 6.18 5.5 10jan2008 6.38 5.5 11jan2008 6.08 5.53 14jan2008 6.25 5.65 15jan2008 6.25 5.65 16jan2008 5.8 5.6 17jan2008 5.88 5.55 18jan2008 5.88 5.75 21jan2008 6.25 5.88 22jan2008 6.75 6.35 23jan2008 6.65 6.25 What I need to do is to create three more general variables, MONTH1-MONTH2 -MONTH3, that capture the price for the next month (the second next etc.) at a given date etc. I was thinking that this might be done by asking Stata to look through the 12 month variables I have, and for the variable MONTH1 then replace this with the first observation not missing. Then for the Variable MONTH2 replace with the SECOND observation of the 12 variables not missing and so on-- With the displayed data above this means that MONTH1 would be 7.6 for 03Dec2007, and 6.73 for 02Jan2008, MONTH2 would be 5.6 for 03Dec2008 and 6 for 02Jan2008, etc. My data is large, so it would be great to have a more general command to create these variables. Hope someone is able to understand this explanation! * * 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/

**References**:**st: Create variable conditional on several others***From:*Linn Renée Naper <linn.naper@ecgroup.no>

**st: RE: Create variable conditional on several others***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**Re: st: Questions on a meta-regression problem** - Next by Date:
**st: Goodness of fit measure akin to R-squared for 0-constant or noconstant** - Previous by thread:
**st: RE: Create variable conditional on several others** - Next by thread:
**re: st: Create variable conditional on several others** - Index(es):

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