I am trying to do a loop over multiple variables to create a histogram for caller frequency to a hotline, and over the histogram I would like to draw lines depending whether or not an advertisement airs on TV for the n-th time in the day. Please see below the post the code, and attached a sample, randomly generated dataset for use with the code (.txt tab delimited form; new here, sorry I can't seem to upload .dta files)
Every day, between 0 and 10 ads will air. I would like to shorten my code so that instead of doing 10 if/else's, I can just loop and insert an extra (scatteri 0 `tc`i'' 30 `tc`i'') whenever ad_`i' exists). Is there a way of doing this?
Thank you very much!
Variable descriptions
dd : date
access: hotline access timestamp
clock : %tc format of access
ad_n : advertisement airtime, n-th time of the day
Code:
forvalues d = 1/31{ forvalues i = 1/10{ sum ad_`i' if dd== `d' local tc`d'`i' `r(mean)' } graph twoway (hist clock if dd==1, frequency width(1800000) start(0)) forvalues i = 1/10{ capture confirm numeric variable ad_`i' if _rc==0{ (scatteri 0 `tc`d'`i'' 30 `tc`d'`i'', c(l) msymbol(i)) //for each of the variables that exists, replicate this section into the graph code } else{ //no command if variable doesn't exist// } } ,title("hotline access on date 1") yscale(range(0 50)) /// ylabel(0(5)50, alt labs(small)) ytick(0(5)50) xlabel(0(7200000)86400000, angle(vertical) format(%tcHH.mm)) /// xtick(0(1800000)86400000) }
0 Response to Code segment conditional on variable existence
Post a Comment