Announcement

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

  • type mismatch

    I have a panel dataset of the total fertility rate, need to take a lag of it and then midpoint. Used commands:

    gen lagged = TFR[_n-1]
    destring TFR Year, replace
    destring lagged, replace
    drop if Year == 1950
    rename _merge _merge1
    gen midpoint = (TFR+lagged)/2

    but it says type mismatch when I input the drop if year==1950 command.
    What should I do?

  • #2
    Tough to know for sure without a data example, but I assume the -destring- didn't work because there are nonnumeric characters in year. Adding the -force- option to the destring will convert all observations with nonnumeric characters into missings, and all observations which are completely numeric into numbers.

    Comment


    • #3
      Hi Syeda,

      It is a little difficult to know for sure without looking at the dataset. Perhaps you could provide an excerpt using dataex if the suggestion below doesn't work? See here: https://www.stata.com/help.cgi?dataex

      My guess is that you have capitalized the Y in Year on the line "drop if Year == 1950". Try changing it to "drop if year == 1950". If the problem is not with the variable name, then try running "desc Year". If the variable type is not a number, then you will need to turn it into a number with "destring Year, replace".

      Another thing to look out for: make sure your dataset is properly sorted before running "gen lagged = TFR[_n-1]" -- I would recommend using the tsset command and then getting the lagged value with "gen lagged = L.TFR". Otherwise, the lagged values aren't going to be correct.

      Best,

      Julian
      Last edited by Julian Duggan; 09 Apr 2021, 11:54.

      Comment

      Working...
      X