Announcement

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

  • Generating a new variable with an if command

    Hi all,

    I have a series of 6 datasets - for ease of reference, let's call them 2015, 2016, 2017, 2018, 2019, 2020.

    I have appended the datasets to make my life a bit easier, however, I am having trouble when creating a real wage variable for each year (the inflation differs across each year, hence I can't apply the same inflation rate across all years).

    In my appended dataset, I created a variable "year" to indicate which year the observation came from.

    I used the following code to generate real wages for 2015

    Code:
    gen realearnings_2015=( s5earnings_employees/74.1)*100 if inlist(year, "2015")
    however, I get the following error message:

    Code:
    type mismatch

  • #2
    Here you have the -if- qualifier and not the if command. The error is consistent with the variable year being a numerical variable and not a string variable as you are treating it with

    inlist(year, "2015")
    by including the double quotes. No need for the -inlist()- function if you only have one element. Be direct.

    Code:
    gen realearnings_2015=(s5earnings_employees/74.1)*100 if year==2015

    Comment


    • #3
      Chris:
      try:
      Code:
      gen realearnings_2015=( s5earnings_employees/74.1)*100 if inlist(year, 2015)
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Cosider the following approach.
        Code:
        generate factor = .
        replace factor = 74.1 if year==2015
        // similar replace commands for 2016-2020
        generate real_earnings = (s5earnings_employees/factor)*100
        The, consistent with what you have for nominal wages (s5earnings_employees), and I expect for all your other variables in the appended dataset, you will have one variable with real wages (real_earnings) in all years.

        Comment

        Working...
        X