Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Lots of date vars from month, day, year vars-FU qx for Eric Wruck


From   "Eric G. Wruck" <ewruck@econalytics.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Lots of date vars from month, day, year vars-FU qx for Eric Wruck
Date   Sat, 12 Nov 2005 14:44:30 -0500

Hi Clare:

No, foreach won't work that way.  Here's an example using forvalues that might help:

     +-----------------------------------------------------------+
     | mo1   day1    yr1   mo2   day2    yr2   mo3   day3    yr3 |
     |-----------------------------------------------------------|
  1. |   1      1   1960    12     25   1965     6      7   1968 |
  2. |  11     12   1907     6     29   1957     3      3   2002 |
     +-----------------------------------------------------------+

. forvalues i=1/3 {
  2.   gen date`i' = mdy(mo`i', day`i', yr`i')
  3. }


. format date* %dd-m-CY

. l

     +----------------------------------------------------------------------------------------------------+
     | mo1   day1    yr1   mo2   day2    yr2   mo3   day3    yr3         date1         date2        date3 |
     |----------------------------------------------------------------------------------------------------|
  1. |   1      1   1960    12     25   1965     6      7   1968    1-Jan-1960   25-Dec-1965   7-Jun-1968 |
  2. |  11     12   1907     6     29   1957     3      3   2002   12-Nov-1907   29-Jun-1957   3-Mar-2002 |
     +----------------------------------------------------------------------------------------------------+

Best wishes,

Eric

 


>Hello, Eric.  Thank you very much for thinking about this.
>
>At 2:02 PM -0500 11/12/05, Eric G. Wruck wrote:
>>I'm not sure I fully understand what you're looking to do (more detail would increase the likelihood that someone will be able to help you).  Nevertheless, you may be interested in Stata's mdy() function.
>
>Yes, the mdy() function is what I can use on three variables at a time.  The problem is that I have as many as 50 triplets of m, d, y that I would like to feed into some kind of automated routine to create 50 corresponding date vars.  Calculation of dates is the simplest problem of this structure to describe, but there are others less trivial, and that is what is really holding me up.
>
>>Eric
>>
>>p.s.  If you have lots of different m, d, & y variables, you may want to learn about foreach or forvalues depending on how your variables are named.
>
>I have done my best to see how foreach could be adapted to this problem.  It is possible that if I understood macros better it would help.  A miniature version of what I am talking about would be variables mo1, mo2, day1, day2, yr1, yr2, hoping to use them to calculate dt1, dt2, without two statements gen dt1 = mdy(mo1,day1,yr1) and gen dt2 = mdy(mo2,day2,yr2).  I'd like to be able to say something like
>
>foreach w of varlist mo1 mo2
> and each x of varlist day1 day2
> and each y of varlist yr1 yr2
> and each z of newlist dt1 dt2 {
>gen z = mdy(w,x,y)
>}
>
>Any idea how I could go about this?
>
>Thank you so much!
>
>				Yours truly,
>				Clare Maxwell


-- 

===================================================

       Eric G. Wruck
       Econalytics
       2535 Sherwood Road
       Columbus, OH  43209

       ph:      614.231.5034
       cell:    614.330.8846
       eFax:    614.573.6639
       eMail:   ewruck@econalytics.com
       website: http://www.econalytics.com

====================================================
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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