Announcement

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

  • Generate continuously count variable for different outcomes

    Dear all

    I'm using Stata 15.1 and with a dataset with Covid19 lockdown measures by US state and date (see example below).
    I am trying to generate a variable that continuously counts the numbers of days a certain lockdown measure was in place.
    E.g. variables "schoolclose_e_nomeas" "schoolclose_e_recoms" "schoolclose_e_reqsome" "schoolclose_e_reqall" are coded 0 when measure was not in place and 1 when measure was in place.
    I have then tried to generate e.g. "schoolclose_e_nomeas_days" to count "schoolclose_e_nomeas" using:
    Code:
     bysort regionname: gen schoolclose_e_nomeas_days = _n if schoolclose_e_nomeas<. & schoolclose_e_nomeas>0
    replace schoolclose_e_nomeas_days = 0 if schoolclose_e_nomeas==0
    But I have two questions/problems:
    1. when variable "schoolclose_e_nomeas" takes the value 0 the new variables has the value '.', but I would like it to take the 'count value': e.g. for "Alabama" 20200313 I would like it to be '72'.
    2. my next problem arises when I generate the next variable schoolclose_e_recoms_days as I want the count to start over from zero, but it countinues from the variable "schoolclose_e_nomeas_days" ie. from 72.
    Code:
    bysort regionname: gen schoolclose_e_recoms_days = _n if schoolclose_e_recoms<. & schoolclose_e_recoms>0
    I have inserted a picture below of how I hoped to generate the variables.
    I hope description makes sense!

    Kind regards,
    Jannie


    DATASET:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 regionname long date float(schoolclose_e_nomeas schoolclose_e_nomeas_days schoolclose_e_recoms schoolclose_e_recoms_days schoolclose_e_reqsome schoolclose_e_reqsome_days schoolclose_e_reqall schoolclose_e_reqall_days)
    "Alabama" 20200101 1  1 0  . 0  . 0   .
    "Alabama" 20200102 1  2 0  . 0  . 0   .
    "Alabama" 20200103 1  3 0  . 0  . 0   .
    "Alabama" 20200104 1  4 0  . 0  . 0   .
    "Alabama" 20200105 1  5 0  . 0  . 0   .
    "Alabama" 20200106 1  6 0  . 0  . 0   .
    "Alabama" 20200107 1  7 0  . 0  . 0   .
    "Alabama" 20200108 1  8 0  . 0  . 0   .
    "Alabama" 20200109 1  9 0  . 0  . 0   .
    "Alabama" 20200110 1 10 0  . 0  . 0   .
    "Alabama" 20200111 1 11 0  . 0  . 0   .
    "Alabama" 20200112 1 12 0  . 0  . 0   .
    "Alabama" 20200113 1 13 0  . 0  . 0   .
    "Alabama" 20200114 1 14 0  . 0  . 0   .
    "Alabama" 20200115 1 15 0  . 0  . 0   .
    "Alabama" 20200116 1 16 0  . 0  . 0   .
    "Alabama" 20200117 1 17 0  . 0  . 0   .
    "Alabama" 20200118 1 18 0  . 0  . 0   .
    "Alabama" 20200119 1 19 0  . 0  . 0   .
    "Alabama" 20200120 1 20 0  . 0  . 0   .
    "Alabama" 20200121 1 21 0  . 0  . 0   .
    "Alabama" 20200122 1 22 0  . 0  . 0   .
    "Alabama" 20200123 1 23 0  . 0  . 0   .
    "Alabama" 20200124 1 24 0  . 0  . 0   .
    "Alabama" 20200125 1 25 0  . 0  . 0   .
    "Alabama" 20200126 1 26 0  . 0  . 0   .
    "Alabama" 20200127 1 27 0  . 0  . 0   .
    "Alabama" 20200128 1 28 0  . 0  . 0   .
    "Alabama" 20200129 1 29 0  . 0  . 0   .
    "Alabama" 20200130 1 30 0  . 0  . 0   .
    "Alabama" 20200131 1 31 0  . 0  . 0   .
    "Alabama" 20200201 1 32 0  . 0  . 0   .
    "Alabama" 20200202 1 33 0  . 0  . 0   .
    "Alabama" 20200203 1 34 0  . 0  . 0   .
    "Alabama" 20200204 1 35 0  . 0  . 0   .
    "Alabama" 20200205 1 36 0  . 0  . 0   .
    "Alabama" 20200206 1 37 0  . 0  . 0   .
    "Alabama" 20200207 1 38 0  . 0  . 0   .
    "Alabama" 20200208 1 39 0  . 0  . 0   .
    "Alabama" 20200209 1 40 0  . 0  . 0   .
    "Alabama" 20200210 1 41 0  . 0  . 0   .
    "Alabama" 20200211 1 42 0  . 0  . 0   .
    "Alabama" 20200212 1 43 0  . 0  . 0   .
    "Alabama" 20200213 1 44 0  . 0  . 0   .
    "Alabama" 20200214 1 45 0  . 0  . 0   .
    "Alabama" 20200215 1 46 0  . 0  . 0   .
    "Alabama" 20200216 1 47 0  . 0  . 0   .
    "Alabama" 20200217 1 48 0  . 0  . 0   .
    "Alabama" 20200218 1 49 0  . 0  . 0   .
    "Alabama" 20200219 1 50 0  . 0  . 0   .
    "Alabama" 20200220 1 51 0  . 0  . 0   .
    "Alabama" 20200221 1 52 0  . 0  . 0   .
    "Alabama" 20200222 1 53 0  . 0  . 0   .
    "Alabama" 20200223 1 54 0  . 0  . 0   .
    "Alabama" 20200224 1 55 0  . 0  . 0   .
    "Alabama" 20200225 1 56 0  . 0  . 0   .
    "Alabama" 20200226 1 57 0  . 0  . 0   .
    "Alabama" 20200227 1 58 0  . 0  . 0   .
    "Alabama" 20200228 1 59 0  . 0  . 0   .
    "Alabama" 20200229 1 60 0  . 0  . 0   .
    "Alabama" 20200301 1 61 0  . 0  . 0   .
    "Alabama" 20200302 1 62 0  . 0  . 0   .
    "Alabama" 20200303 1 63 0  . 0  . 0   .
    "Alabama" 20200304 1 64 0  . 0  . 0   .
    "Alabama" 20200305 1 65 0  . 0  . 0   .
    "Alabama" 20200306 1 66 0  . 0  . 0   .
    "Alabama" 20200307 1 67 0  . 0  . 0   .
    "Alabama" 20200308 1 68 0  . 0  . 0   .
    "Alabama" 20200309 1 69 0  . 0  . 0   .
    "Alabama" 20200310 1 70 0  . 0  . 0   .
    "Alabama" 20200311 1 71 0  . 0  . 0   .
    "Alabama" 20200312 1 72 0  . 0  . 0   .
    "Alabama" 20200313 0  . 1 73 0  . 0   .
    "Alabama" 20200314 0  . 1 74 0  . 0   .
    "Alabama" 20200315 0  . 1 75 0  . 0   .
    "Alabama" 20200316 0  . 1 76 0  . 0   .
    "Alabama" 20200317 0  . 1 77 0  . 0   .
    "Alabama" 20200318 0  . 1 78 0  . 0   .
    "Alabama" 20200319 0  . 0  . 1 79 0   .
    "Alabama" 20200320 0  . 0  . 0  . 1  80
    "Alabama" 20200321 0  . 0  . 0  . 1  81
    "Alabama" 20200322 0  . 0  . 0  . 1  82
    "Alabama" 20200323 0  . 0  . 0  . 1  83
    "Alabama" 20200324 0  . 0  . 0  . 1  84
    "Alabama" 20200325 0  . 0  . 0  . 1  85
    "Alabama" 20200326 0  . 0  . 0  . 1  86
    "Alabama" 20200327 0  . 0  . 0  . 1  87
    "Alabama" 20200328 0  . 0  . 0  . 1  88
    "Alabama" 20200329 0  . 0  . 0  . 1  89
    "Alabama" 20200330 0  . 0  . 0  . 1  90
    "Alabama" 20200331 0  . 0  . 0  . 1  91
    "Alabama" 20200401 0  . 0  . 0  . 1  92
    "Alabama" 20200402 0  . 0  . 0  . 1  93
    "Alabama" 20200403 0  . 0  . 0  . 1  94
    "Alabama" 20200404 0  . 0  . 0  . 1  95
    "Alabama" 20200405 0  . 0  . 0  . 1  96
    "Alabama" 20200406 0  . 0  . 0  . 1  97
    "Alabama" 20200407 0  . 0  . 0  . 1  98
    "Alabama" 20200408 0  . 0  . 0  . 1  99
    "Alabama" 20200409 0  . 0  . 0  . 1 100
    end
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	66.6 KB
ID:	1750269

  • #2
    Dear Jannie,

    probably there are many more elegant solutions to your problem but I think the following will work, too ;-)
    As I could only input your example, I had to drop your wrong *_days variables first. Apologies for that.

    Code:
    capture: drop *_days  // dropping the wrong _days-variables ;-)
    unab daycountvars : schoolclose*            // use local for looping over schoolclose-variables
    foreach daycountvar of local daycountvars {
        // generate number within group only for cases where schoolclose-variable equals 1
        bysort regionname `daycountvar' (date): gen `daycountvar'_days = _n if `daycountvar'==1        
        bysort regionname (date): replace `daycountvar'_day = `daycountvar'_day[_n-1] if missing(`daycountvar'_days) & !missing(`daycountvar'_days[_n-1])    // replace by value of the previous observation within group when previous is not missing
        order `daycountvar'_day, after(`daycountvar')        // order new variable after original one
    }
    I assume that, unlike in the picture, you also wanted to have the total number of closed days for variable schoolclose_e_reqsome for all days after 20200319, right?

    Kind regards,
    Benno
    Last edited by Benno Schoenberger; 17 Apr 2024, 06:39.

    Comment


    • #3
      Thanks Benno Schoenberger ! This is exactly what I was looking for. And yes, I want to continue for all dates (also after 20200319) such that I at any date, can see how many days certain closures had lasted by e.g. 20220305.

      Comment


      • #4

        Hi Jannie, good that you work with Statas default "set varabbreviate on" ;-). I just realized that I have hidden three typos in my code. Of course, instead of *_day it should be *_days everywhere.

        Comment

        Working...
        X