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]

st: RE: converting date format


From   Red Owl <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: converting date format
Date   Mon, 22 Jul 2013 18:52:47 +0000

This could probably be simplified, but the following works.

* Generate toy data set.
clear
input str10 olddate
  "01/01/1947"         
  "04/01/1947"         
  "07/01/1947"         
  "01/01/2012"         
  "04/01/2012"         
  "07/01/2012"         
  "10/01/2012"
end

* Create new variable for year and quarter
gen mo = real(substr(olddate,1,2))
gen str6 newdate=substr(olddate,7,4)+"q1"
replace newdate = substr(olddate,7,4)+"q2" if mo > 3 & mo < 7 
replace newdate = substr(olddate,7,4)+"q3" if mo > 6 & mo < 10 
replace newdate = substr(olddate,7,4)+"q4" if mo >= 10 
drop mo

* List data
list _all, noobs sep(0)

Red Owl


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index