Announcement

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

  • Converting quarterly to annual data using the first quarter

    I know how to convert quarterly to annual data using the collapse command. However, I can't seem to find any examples of converting quarterly data to annual, using the first quarter's data only, which is what I want. Does anyone know how to go about doing that?
    Data example (of a limited portion of the data), with the yearly ids generated by me:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int omr float(year t) long hus
    101 2008 192 26852
    101 2008 193 26058
    101 2008 194 24502
    101 2008 195 22342
    101 2009 196 20825
    101 2009 197 20847
    101 2009 198 21954
    101 2009 199 22090
    101 2010 200 22775
    101 2010 201 24160
    101 2010 202 24260
    101 2010 203 24357
    101 2011 204 23203
    101 2011 205 25144
    101 2011 206 24307
    101 2011 207 22525
    101 2012 208 22289
    101 2012 209 23782
    101 2012 210 23343
    101 2012 211 22932
    101 2013 212 24217
    101 2013 213 24611
    101 2013 214 24334
    101 2013 215 25883
    101 2014 216 25429
    101 2014 217 26568
    101 2014 218 27162
    101 2014 219 27951
    101 2015 220 28418
    101 2015 221 29930
    101 2015 222 30276
    101 2015 223 29875
    101 2016 224 31561
    101 2016 225 31159
    101 2016 226 32114
    101 2016 227 30752
    101 2017 228 32044
    101 2017 229 33937
    101 2017 230 34306
    101 2017 231 34013
    101 2018 232 36761
    101 2018 233 35334
    101 2018 234 35701
    101 2018 235 37494
    101 2019 236 36347
    101 2019 237 36170
    101 2019 238 37003
    101 2019 239 38894
    101 2020 240 37680
    101 2020 241 38803
    101 2020 242 40458
    101 2020 243 42173
    101 2021 244 45636
    101 2021 245 46966
    101 2021 246 49455
    101 2021 247 47726
    101 2022 248 48374
    101 2022 249 50514
    101 2022 250 49173
    101 2022 251 46075
    101 2023 252 44157
    101 2023 253 46272
    147 2008 192 39382
    147 2008 193 36224
    147 2008 194 32944
    147 2008 195 31799
    147 2009 196 24743
    147 2009 197 30706
    147 2009 198 27445
    147 2009 199 26027
    147 2010 200 32509
    147 2010 201 34462
    147 2010 202 32431
    147 2010 203 35141
    147 2011 204 37246
    147 2011 205 37048
    147 2011 206 25077
    147 2011 207 28405
    147 2012 208 27052
    147 2012 209 27321
    147 2012 210 33137
    147 2012 211 34696
    147 2013 212 35653
    147 2013 213 34015
    147 2013 214 30697
    147 2013 215 33955
    147 2014 216 38057
    147 2014 217 35275
    147 2014 218 36666
    147 2014 219 38240
    147 2015 220 39828
    147 2015 221 43955
    147 2015 222 40725
    147 2015 223 43772
    147 2016 224 35664
    147 2016 225 44624
    147 2016 226 42008
    147 2016 227 42545
    147 2017 228 46969
    147 2017 229 43342
    end

  • #2
    Well, if your data is never missing a first quarter for any omr in any year, you can do this:
    Code:
    sort omr t
    collapse (first) hus, by(omr year)
    But you should probably verify that assumption before proceeding or you will end up with unwanted second or later quarter results being used instead. So, I would precede the above code with -by omr (t), sort: assert quarter(dofq(t[1])) == 1-.

    Comment


    • #3
      A little fooling around establishes that a necessary and sufficient test for qdate indicating the first quarter is that mod(qdate, 4) is 0. This also follows directly from the definition that the first quarter of 1960 was 0, so all later and earlier first quarters differ by a multiple of 4.


      Code:
      gen wanted = cond(mod(t, 4) == 0, hus, .) 
      collapse wanted, by(omr year)

      Comment

      Working...
      X