Stata - Loop with Time Stamps and Alert to Your Phone

Stata - Loop with Time Stamps and Alert to Your Phone

Xinya HAO (Hall)

Code block for looping the files in a folder with time stamps

It also allows you to send a message to your Slack APP channel when the program is done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
* use log
cap log close _all
log using "_log_file.smcl", replace

* set the folder path and file type for loop
local folder_path = "./the_folder_path"
local file_type = ".dta"
local files: dir "`folder_path'" files "*`file_type'"
local files_len : word count `files'

local i = 1
scalar _total_time = 0
scalar _avg_time = 0
foreach file of local files {
local pref = substr("`file'", 1, 1)
qui if "`pref'" != "." {
cap timer clear 1
timer on 1

* Do something


timer off 1
timer list 1
scalar _total_time = round(r(t1) + `=scalar(_total_time)' , 0.001)
local __display_total_time : dis %18.3f = _total_time
local __display_total_time = trim("`__display_total_time'")
scalar _avg_time = round(_total_time / `i' , 0.001)
local __display_avg_time : dis %18.3f = _avg_time
local __display_avg_time = trim("`__display_avg_time'")
nois dis as text ///
"`i'/`files_len' Done. Time used: " r(t1) " || Total tile used: `__display_total_time' || Avg time per it: `__display_avg_time'"
}
local ++i
}
sendtoslack, ///
url(put your url here) ///
method(curl) ///
message("Your Program is Done! Total Time Used: `__display_total_time'")
di "Pgm End Time: " c(current_date) " " c(current_time)
cap log close _all

sendtoslack

The program use sendtoslack to send a message to your slack channel.

1
2
ssc install sendtoslack
help sendtoslack
Comments
On this page
Stata - Loop with Time Stamps and Alert to Your Phone