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: Efficient code to generate many date/time variables at once
From
"Radwin, David" <[email protected]>
To
"[email protected]" <[email protected]>
Subject
st: RE: Efficient code to generate many date/time variables at once
Date
Tue, 25 Mar 2014 23:54:37 +0000
Alison,
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).
David
--
David Radwin, Senior Research Associate
Education and Workforce Development
RTI International
2150 Shattuck Ave. Suite 800, Berkeley, CA 94704
Phone: 510-665-8274
www.rti.org/education
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Alison El Ayadi
> Sent: Tuesday, March 25, 2014 4:13 PM
> To: [email protected]
> Subject: st: Efficient code to generate many date/time variables at once
>
> Dear Statalisters -
>
> 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?
>
> Thanks,
> Alison
*
* 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/