Announcement

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

  • Logic operations with generate

    I have a database with three variables: 27 states, 10 years and a GDP for each year. I want to generate a variable with the resulting logical operation: if year=2017, then GDP*2 & if year=2018, then GDP*7 & if year=2019, then GDP*15, and so on.
    How can I do that, please?

  • #2
    Code:
    gen new_variable = GDP*2 if year == 2017
    replace new_variable = GDP*7 if year == 2018
    replace new_variable = GDP*15 if year == 2019
    // "and so on."
    I have no idea what "and so on" means here. There is no obvious pattern to the numbers 2, 7, and 15. If there were (or if I perceived it), one could probably do this with more compact code. But with non-systematic multipliers, the only thing that might be more suitable is to create a data set that crosswalks the year with multiplier, merge that with the original database, and then just -gen new_variable = GDP * multiplier-.

    By the way, since you start your question with year == 2017, how do you have 10 years of data? What data do you have for years after 2022?

    Comment


    • #3
      Thanks a lot for your reply, Clyde. You helped a lot!
      By the way, I have 10 observations of year (2012-2021). Each state has 10 observations of year, therefore each state has a value of GDP for each year. I used the numbers 2, 7 and 15 just for giving an example; they're pointless.

      Comment

      Working...
      X