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]
RE: st: RE: Efficient code to generate many date/time variables at once
From
Andrew Maurer <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: RE: Efficient code to generate many date/time variables at once
Date
Wed, 26 Mar 2014 14:36:08 +0000
If you're worried about computational efficiency, it doesn't sound very efficient to convert the td portion to a string, only to convert it back to numeric. Here's a version that's roughly 4x faster in one test (the "faster method" below):
**************** begin code *************
clear all
set obs 1000000
// fake some data
foreach v in drape curtain blind shade {
gen dt_`v' = td(1jan2000) + int(1000*runiform())
gen tm_`v' = "11:15"
}
// earlier method
timer on 1
foreach v in drape curtain blind shade {
local work string(dt_`v',"%td") + " " + tm_`v'
gen double dt_tm_`v' = clock(`work',"DMYhm")
format dt_tm_`v' %tc
}
timer off 1
// faster method
timer on 2
foreach v in drape curtain blind shade {
gen double dt_tm_`v'2 = cofd(dt_`v') + clock(tm_`v',"hm")
format dt_tm_`v'2 %tc
}
timer off 2
timer list
/*
timer 1: 8.99
timer 2: 2.19
*/
**************** end code ***************
Best,
Andrew Maurer
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Tuesday, March 25, 2014 8:43 PM
To: [email protected]
Subject: Re: st: RE: Efficient code to generate many date/time variables at once
Just a small tweak to David's code will cut down on the number of variables.
foreach v of varlist drape curtain blind shade {
local work string(dt_`v',"%td") + " " + tm_`v'
gen double dt_tm_`v' = clock(`work',"DMYhm")
format dt_tm_`v' %tc
}
In this example, omitting not only the equals sign but also delimiting
quotation marks are important details in the definition of the local
macro.
Nick
[email protected]
Radwin, David
> This sounds like a job for a simple -foreach- loop. You might specify the true varlist with a wildcard (dr*) or as a range of contiguous variables (drape - shade).
>
> foreach v of varlist drape curtain blind shade {
> gen dt_tm_`v'_str = string(dt_`v',"%td") + " " + tm_`v'
> gen double dt_tm_`v' = clock(dt_tm_`v'_str,"DMYhm")
> format dt_tm_`v' %tc
> }
>
> (This code is untested.) You could combine the two -generate- lines to increase computational efficiency, but I don't think that is the kind of efficiency you seek, and doing so probably isn't worth the hassle.
>
> For further reading, see Kit Baum's Why should you become a Stata programmer? (http://fmwww.bc.edu/GStat/docs/StataProg.pdf) or An Introduction to Stata Programming (http://www.stata-press.com/books/introduction-stata-programming).
Alison El Ayadi
>> I have to create a number of date/time variables using a %td formatted
>> date and a string time, which I generally do using the following code,
>> for each of the different time points -
>>
>> gen dt_tm_drape_str = string(dt_drape,"%td") + " " + tm_drape
>> gen double dt_tm_drape = clock(dt_tm_drape_str,"DMYhm")
>> format dt_tm_drape %tc
>>
>> I'd like to make my code more efficient by having it run through all
>> of the variables that I have, which are paired as dt_X and tm_X. Any
>> suggestions on how to put together a program to do this?
*
* 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/
*
* 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/