Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: RE: Efficient code to generate many date/time variables at once |
Date | Wed, 26 Mar 2014 01:43:12 +0000 |
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 njcoxstata@gmail.com 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/