Announcement

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

  • Generating New Observations

    Hello everyone,

    I want to learn if it is possible to generate new observations in the same variables with using the existing variables.
    Assume the following example: Time series data from 1980-2022 and the variable for the years is "Year" and there is "Month" variable 1-12 for each year. Now further assume that we have variables var_1........var_20 .
    I want to create 2023 under the year with 12 observation 1-12 under month. and for all the other variables var_1....var_20 I want to generate the values as the sum of values from 2009 and 2010. I thought it should be something like that.

    Code:
    gen obs(2023) = if Year==2009 + if Year=2010
    Of course such code does not exist and does not work I gave it as an indication to make my question more understandable. Is it possible to do that. I can do it in new variable but I have no idea how can I generate new observations from the existing observations. This example is just for the general case. I have not seen any related topic before so if there are please inform me so that I will follow that topic.

  • #2
    If I understand this correctly you have 43 * 12 = 516 observations. You want an extra 12,


    Code:
    set obs 528
    replace Year = 2023 in -12/L
    replace Month = _n - 516 in -12/L
    Otherwise that sounds a very strange thing to want, but this seems to be what you are asking

    Code:
    forval j = 1/20 {
          su var_`j' if inlist(Year, 2009, 2010), meanonly
          di _n  "var_`j'    "  r(sum)
          replace var_`j' = r(sum) if Year == 2023
    }
    If you want the values for e.g. January 2023 to be the sum of January 2009 and January 2010, then that could be done, but i will not write code at this point.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      If I understand this correctly you have 43 * 12 = 516 observations. You want an extra 12,


      Code:
      set obs 528
      replace Year = 2023 in -12/L
      replace Month = _n - 516 in -12/L
      Otherwise that sounds a very strange thing to want, but this seems to be what you are asking

      Code:
      forval j = 1/20 {
      su var_`j' if inlist(Year, 2009, 2010), meanonly
      di _n "var_`j' " r(sum)
      replace var_`j' = r(sum) if Year == 2023
      }
      If you want the values for e.g. January 2023 to be the sum of January 2009 and January 2010, then that could be done, but i will not write code at this point.
      Thank you so much that is exactly what I ask. I understand it seems strange however for some of the data in the long format that I am dealing with I need to create generated observations using the existing observations. That's why I need a way to be able to generate them. I got the idea and will be able to do rest of it my self you have provided enough detail. Thanks again.

      Comment

      Working...
      X