Statalist


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

Re: st: changing ddmmyyyy to yyyymm


From   Svend Juul <SJ@SOCI.AU.DK>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: changing ddmmyyyy to yyyymm
Date   Mon, 15 Sep 2008 19:19:12 +0200

Fabian Brenner wrote:
 
I have the following problem: 
 
Variable "date" has the following form "11301998" 
(the format is str8 %8s). I want to create a new 
variable called "calculation_date"  (format: str6 %9s). 
Calculation date should have the form "199811".
 
(that means changing date from ddmmyyyy to yyyymm)
 
=========================================================
 
"11301998" is in format mmddyyyy, but I assume that you
do mean ddmmyyyy as stated elsewhere.
 
// Testdata:
   clear
   input str8 date_s
   30111998
   01022003
   24122008
   end
 
// You can use the substr() function to isolate parts of a string
   gen str6 ym_s = substr(date_s,5,4) + substr(date_s,3,2)
 
// but for most purposes date variables are a lot more flexible
// (I assume that you have Stata 10). With -format- you can decide
// the display format:

   gen date_dmy = date(date_s,"DMY")
   format date_dmy %tdDDNNCCYY
 
   gen date_ym = date_dmy
   format date_ym %tdCCYYNN
 
   . list
        +----------------------------------------+
        |   date_s     ym_s   date_dmy   date_ym |
        |----------------------------------------|
     1. | 30111998   199811   30111998    199811 |
     2. | 01022003   200302   01022003    200302 |
     3. | 24122008   200812   24122008    200812 |
        +----------------------------------------+
 
You probably would benefit from studying the output from
  
   . help date
 
Hope this helps
Svend
________________________________________________________ 
 
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6 
DK-8000 Aarhus C,  Denmark 
Phone, work:   +45 8942 6090 
Phone, home:   +45 8693 7796 
Fax:           +45 8613 1580 
E-mail:        sj@soci.au.dk 
_________________________________________________________ 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index