Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Code segment conditional on variable existence

    Hi All,

    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)
    }
    Attached Files
    Last edited by England Rhys; 20 Aug 2019, 00:28.

  • #2
    Please give data examples using
    Code:
    dataex
    as explained in the FAQ Advice.

    You can't have a loop in the middle of a command. I notice that your loop

    Code:
    forvalues i = 1/10{
            sum ad_`i' if dd== `d'
            local tc`d'`i' `r(mean)'
    }
    doesn't check that the variables exist....

    Comment


    • #3
      Hi Nick,

      Thanks for pointing it out, will use dataex next time around.

      You are right, my bad, I tried creating a sample dataset and code in a hurry so I missed the checking for that section.

      Thanks as well for clarifying. So in order to implement what I want, I'll just have to make 10 if/else's, no shorter way around it?

      Comment


      • #4
        Why not now?

        I don't fully understand what you want to do. For example, your loop just draws results for day 1 again and again.

        This may help, however.

        Code:
        local opts yscale(range(0 50)) ///
                   ylabel(0(5)50, alt labs(small)) ///
                   ytick(0(5)50) ///
                   xlabel(0(72e5)864e5, angle(vertical) format(%tcHH.mm)) ///
                   xtick(0(18e5)864e5) ///
                   frequency width(18e5) start(0)
        
        forvalues d = 1/31 {
            local call 
            forvalues i = 1/10 {
                sum ad_`i' if dd== `d', meanonly 
                if r(mean) < . local call `call' || scatteri 0 `r(mean)', recast(line) 
            }    
        
            twoway hist clock if dd == `d', `opts' title("hotline access on date `d'")  || `call' 
        }

        Comment


        • #5
          Hi Nick,

          Sorry for the lack of clarity and confusion.

          Yes that's another mistake I made there, I copied that line from the original code I was testing just for one day before implementing the loop over a number of days.

          So ideally I would want a graph as attached. The two vertical lines are drawn by my scatteri plots, and I want there to be the same number of lines as there are number of advertisements in one day (say, n), so I would want the scatteri plot (1) pasted in behind the histogram plot segment n times, and (2) for each of the n times the local name changed accordingly. I use the summary and pull the means into the locals because it is the only way I figured out to input a date/time value as an input into my scatteri plot.

          I also don't quite understand what the local call does in the code, and have tried looking around and outside the forum, no luck so far. Could you please explain what it does generally and in this context?

          Below are my sample data and revised code with which I created the graph.

          Many thanks for taking your time to help.

          -England


          Click image for larger version

Name:	exgraph.png
Views:	2
Size:	35.3 KB
ID:	1512967




          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float dd str8 access float(clock ad_1 ad_2 ad_3 ad_4 ad_5 ad_6 ad_7 ad_8 ad_9 ad_10)
          1 "11:35:00" 4.17e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "19:21:00" 69660000 4.95e+07 5.40e+07 . . . . . . . .
          1 "00:17:00"  1020000 4.95e+07 5.40e+07 . . . . . . . .
          1 "00:41:00"  2460000 4.95e+07 5.40e+07 . . . . . . . .
          1 "05:43:00" 20580000 4.95e+07 5.40e+07 . . . . . . . .
          1 "23:51:00" 85860000 4.95e+07 5.40e+07 . . . . . . . .
          1 "15:27:00" 55620000 4.95e+07 5.40e+07 . . . . . . . .
          1 "07:15:00" 2.61e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "06:52:00" 24720000 4.95e+07 5.40e+07 . . . . . . . .
          1 "03:34:00" 12840000 4.95e+07 5.40e+07 . . . . . . . .
          1 "02:22:00"  8520000 4.95e+07 5.40e+07 . . . . . . . .
          1 "12:05:00" 4.35e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "18:53:00" 67980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "20:01:00" 72060000 4.95e+07 5.40e+07 . . . . . . . .
          1 "20:37:00" 74220000 4.95e+07 5.40e+07 . . . . . . . .
          1 "06:57:00" 25020000 4.95e+07 5.40e+07 . . . . . . . .
          1 "18:59:00" 68340000 4.95e+07 5.40e+07 . . . . . . . .
          1 "21:33:00" 77580000 4.95e+07 5.40e+07 . . . . . . . .
          1 "05:46:00" 20760000 4.95e+07 5.40e+07 . . . . . . . .
          1 "22:18:00" 80280000 4.95e+07 5.40e+07 . . . . . . . .
          1 "14:45:00" 5.31e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "22:41:00" 81660000 4.95e+07 5.40e+07 . . . . . . . .
          1 "01:32:00"  5520000 4.95e+07 5.40e+07 . . . . . . . .
          1 "14:28:00" 52080000 4.95e+07 5.40e+07 . . . . . . . .
          1 "17:59:00" 64740000 4.95e+07 5.40e+07 . . . . . . . .
          1 "19:29:00" 70140000 4.95e+07 5.40e+07 . . . . . . . .
          1 "04:29:00" 16140000 4.95e+07 5.40e+07 . . . . . . . .
          1 "12:57:00" 46620000 4.95e+07 5.40e+07 . . . . . . . .
          1 "17:07:00" 61620000 4.95e+07 5.40e+07 . . . . . . . .
          1 "16:01:00" 57660000 4.95e+07 5.40e+07 . . . . . . . .
          1 "19:34:00" 70440000 4.95e+07 5.40e+07 . . . . . . . .
          1 "08:42:00" 31320000 4.95e+07 5.40e+07 . . . . . . . .
          1 "07:39:00" 27540000 4.95e+07 5.40e+07 . . . . . . . .
          1 "15:56:00" 57360000 4.95e+07 5.40e+07 . . . . . . . .
          1 "14:04:00" 50640000 4.95e+07 5.40e+07 . . . . . . . .
          1 "18:16:00" 65760000 4.95e+07 5.40e+07 . . . . . . . .
          1 "05:09:00" 18540000 4.95e+07 5.40e+07 . . . . . . . .
          1 "04:23:00" 15780000 4.95e+07 5.40e+07 . . . . . . . .
          1 "09:28:00" 34080000 4.95e+07 5.40e+07 . . . . . . . .
          1 "12:45:00" 4.59e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "19:52:00" 71520000 4.95e+07 5.40e+07 . . . . . . . .
          1 "07:03:00" 25380000 4.95e+07 5.40e+07 . . . . . . . .
          1 "16:29:00" 59340000 4.95e+07 5.40e+07 . . . . . . . .
          1 "13:53:00" 49980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "23:23:00" 84180000 4.95e+07 5.40e+07 . . . . . . . .
          1 "18:03:00" 64980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "21:25:00" 7.71e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "08:27:00" 30420000 4.95e+07 5.40e+07 . . . . . . . .
          1 "11:29:00" 41340000 4.95e+07 5.40e+07 . . . . . . . .
          1 "05:05:00" 1.83e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "23:25:00" 8.43e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "12:57:00" 46620000 4.95e+07 5.40e+07 . . . . . . . .
          1 "03:03:00" 10980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "00:09:00"   540000 4.95e+07 5.40e+07 . . . . . . . .
          1 "01:29:00"  5340000 4.95e+07 5.40e+07 . . . . . . . .
          1 "11:07:00" 40020000 4.95e+07 5.40e+07 . . . . . . . .
          1 "22:06:00" 79560000 4.95e+07 5.40e+07 . . . . . . . .
          1 "00:04:00"   240000 4.95e+07 5.40e+07 . . . . . . . .
          1 "23:12:00" 83520000 4.95e+07 5.40e+07 . . . . . . . .
          1 "17:13:00" 61980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "22:06:00" 79560000 4.95e+07 5.40e+07 . . . . . . . .
          1 "06:37:00" 23820000 4.95e+07 5.40e+07 . . . . . . . .
          1 "17:13:00" 61980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "00:05:00"   300000 4.95e+07 5.40e+07 . . . . . . . .
          1 "19:26:00" 69960000 4.95e+07 5.40e+07 . . . . . . . .
          1 "02:55:00" 1.05e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "05:27:00" 19620000 4.95e+07 5.40e+07 . . . . . . . .
          1 "20:04:00" 72240000 4.95e+07 5.40e+07 . . . . . . . .
          1 "03:13:00" 11580000 4.95e+07 5.40e+07 . . . . . . . .
          1 "21:32:00" 77520000 4.95e+07 5.40e+07 . . . . . . . .
          1 "07:05:00" 2.55e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "23:44:00" 85440000 4.95e+07 5.40e+07 . . . . . . . .
          1 "03:03:00" 10980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "05:29:00" 19740000 4.95e+07 5.40e+07 . . . . . . . .
          1 "16:51:00" 60660000 4.95e+07 5.40e+07 . . . . . . . .
          1 "02:43:00"  9780000 4.95e+07 5.40e+07 . . . . . . . .
          1 "12:59:00" 46740000 4.95e+07 5.40e+07 . . . . . . . .
          1 "10:14:00" 36840000 4.95e+07 5.40e+07 . . . . . . . .
          1 "12:31:00" 45060000 4.95e+07 5.40e+07 . . . . . . . .
          1 "03:18:00" 11880000 4.95e+07 5.40e+07 . . . . . . . .
          1 "14:04:00" 50640000 4.95e+07 5.40e+07 . . . . . . . .
          1 "14:24:00" 51840000 4.95e+07 5.40e+07 . . . . . . . .
          1 "07:13:00" 25980000 4.95e+07 5.40e+07 . . . . . . . .
          1 "21:52:00" 78720000 4.95e+07 5.40e+07 . . . . . . . .
          1 "07:11:00" 25860000 4.95e+07 5.40e+07 . . . . . . . .
          1 "08:38:00" 31080000 4.95e+07 5.40e+07 . . . . . . . .
          1 "19:05:00" 6.87e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "14:47:00" 53220000 4.95e+07 5.40e+07 . . . . . . . .
          1 "15:41:00" 56460000 4.95e+07 5.40e+07 . . . . . . . .
          1 "12:55:00" 4.65e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "07:05:00" 2.55e+07 4.95e+07 5.40e+07 . . . . . . . .
          1 "10:31:00" 37860000 4.95e+07 5.40e+07 . . . . . . . .
          1 "08:08:00" 29280000 4.95e+07 5.40e+07 . . . . . . . .
          1 "00:28:00"  1680000 4.95e+07 5.40e+07 . . . . . . . .
          1 "03:12:00" 11520000 4.95e+07 5.40e+07 . . . . . . . .
          1 "23:06:00" 83160000 4.95e+07 5.40e+07 . . . . . . . .
          1 "23:04:00" 83040000 4.95e+07 5.40e+07 . . . . . . . .
          1 "16:09:00" 58140000 4.95e+07 5.40e+07 . . . . . . . .
          1 "15:07:00" 54420000 4.95e+07 5.40e+07 . . . . . . . .
          1 "04:45:00" 1.71e+07 4.95e+07 5.40e+07 . . . . . . . .
          end
          
          
          foreach d in 1{
              forvalues i = 1/10{
                  sum ad_`i'
                  if r(mean)< .{
                      sum ad_`i' if dd== `d'
                      local tc`d'`i' `r(mean)'
                  }
                  else{
                  di "does not exist"
                  }
              }
          
              graph twoway (hist clock if dd==`d', frequency width(1800000) start(0)) ///
                                  (scatteri 0 `tc`d'1' 10 `tc`d'1', c(l) msymbol(i)) /// //I want to create a loop that eliminates the need for me to spell out this line and the next for each and every ad_n !=.
                                  (scatteri 0 `tc`d'2' 10 `tc`d'2', c(l) msymbol(i)) ///
                                  ,title("hotline access on date 1") yscale(range(0 10)) ///
                          ylabel(0(2)10, alt labs(small)) ytick(0(2)10) xlabel(0(7200000)86400000, angle(vertical) format(%tcHH.mm)) ///
                          xtick(0(1800000)86400000) legend(order(1 "Frequency"))
          }
          Attached Files

          Comment


          • #6
            The business with local macro call is just building up a macro by adding content each time around a loop (which we can make conditional on that content being suitable to add). If the macro is being used more than once, it may be prudent to blank it out at the outset. Running this will show the main ideas.

            Code:
            local call 
            
            foreach w in frog toad newt dragon { 
                local call `call' `w' 
                di "`call'"
            }
            
            local call 
            
            forval y = 1/4 { 
                local call `call' `y' 
                di "`call'"
            }

            Comment

            Working...
            X