Announcement

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

  • #31
    The implication of that error message is that your original data contains some values of qdate that are outside the 2000q1-2021q2 range. I assumed that such values of qdate would not exist, but looking back over your original problem description, I see that you never said anything that implied that. So, there are two possibilities:

    1. Your data contains values of qdate outside the 2000q1-2021q2 range, and such values are not allowable--in that case you have review the data management that created you data set and fix it so that those values no longer appear.

    2. It is perfectly OK for the data to have values of qdate outside the 2000q1-2021q2 ranage and I erroneously built into the code an assumption that there shouldn't be any. In that case, just remove the -assert(match using)- option from the -merge- command and proceed. And accept my apology for the incorrect assumption.

    Comment


    • #32
      I have a data for years 2008, 2011, 2014 and 2016. I want the dataset to run from 2008 to 2019. Following this thread, I am able to add years between the years I already have, but now it looks like this 2008 2009 2011 2012 2014 2015 2017 2018.

      The code I use as follows:
      Code:
      expand 3, gen(expandob)
      sort fips expandob year
      ds year fips expandob state, not
      local toempty `r(varlist)' 
      foreach var of local toempty{ 
          replace `var' = . if expandob == 1 
      }
      foreach var of varlist year fips {
          bysort fips: replace year = year[_n-1]+1  if expandob == 1 & year== year[_n-1]
      }
      How do I fix the code to get 2010, 2013, 2016, 2019?

      Can someone please help me?

      Note: I didn't start another thread because my question was most suited to this thread.

      Thank you.

      Comment


      • #33
        Ritika Khurana h tsfill

        Comment


        • #34
          Jared Greathouse this solved my problem. Thank you!

          Comment


          • #35
            Jared Greathouse I just realized, it did work for missing years in between 2008 to 2018 but it did not generate 2019.

            Comment


            • #36
              I'm not at my computer... perhaps this post would help? Ritika Khurana

              Comment


              • #37
                Jared Greathouse Indeed, it did!

                I will post my monkey code here to help others. I am sure there are more sophisticated ways of doing this.
                Code:
                expand 3, gen(expandob)
                sort fips expandob year
                ds year fips expandob state, not
                local toempty `r(varlist)' 
                foreach var of local toempty{ 
                    replace `var' = . if expandob == 1 
                }
                *Generating missing years
                foreach var of varlist year fips {
                    bysort fips: replace year = year[_n-1]+1  if expandob == 1 & year== year[_n-1]
                }
                tab year
                *Dropping duplicate years generated in previous step
                duplicates drop fips year, force
                
                *Setting time-series
                tsset fips year
                
                *Filling up remaining missing years until 2018
                tsfill
                tab year
                
                *Adding year 2019
                bys fips (year): gen x = _n==_N
                expand 2 if x
                bys fips (year): replace year = year + sum(x) if sum(x)>1
                tab year

                Comment

                Working...
                X