Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Add 15 days to a mm/yyyy


From   wgould@stata.com (William Gould, StataCorp LP)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Add 15 days to a mm/yyyy
Date   Thu, 10 Apr 2008 09:21:34 -0500

Unknown <statadndee@yahoo.co.uk> writes, 

> Could anyone please help me with one of the questions?
> 
> Var1- Storage type str8, display format %9s. It has
> month and year only like: 06/2005 .
>
> I want to 
>          1- change the format to time, 
>          2- add day dd/mm/yyyy, and 
>          3- set dd for 15 like say: 15/05/2005.

I created an example dataset:

        . list

             +----------+
             | original |
             |----------|
          1. |  06/2005 |
          2. |  09/2006 |
             +----------+

Variable original is str8.

Here's one solution:

        . gen long newvar = date(original, "MY")

        . format newvar %td

        . list

             +----------------------+
             | original      newvar |
             |----------------------|
          1. |  06/2005   01jun2005 |
          2. |  09/2006   01sep2006 |
             +----------------------+


Variable newvar is a -long-.  If we wanted newvar to display as 
01/06/2005 and 01/09/2005, we would specify format %tdDD/NN/CCYY
rather than %td.

         . format newvar %tdDD/NN/CCYY

         . list
           <output omitted>

To shift newvar 14 days forward:

        . replace newvar = newvar+14
        (2 real changes made)

        . list

             +-----------------------+
             | original       newvar |
             |-----------------------|
          1. |  06/2005   15/06/2005 |
          2. |  09/2006   15/09/2006 |
             +-----------------------+

See -help dates and times-.

-- Bill
wgould@stata.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