0
votes

I am working with Stata and have a panel dataset with years ranging from 1990 to 2015. When browsing the data, the years are displayed as 1990, 1991 and so on. However, for instance, when trying to drop a year, it only works the following way

drop if year==11

which results in dropping the year 2000. When plotting data, the ticks are also displayed as 1,2,3,4...,25, 26, instead of the actual years.

How can I convert back years into their actual values?

1
Note stackoverflow.com/help/mcve as giving the standard for questions. My answer required some guesses. If it's not the correct answer, you'll need to provide more information.Nick Cox

1 Answers

0
votes

It sounds as if you or someone else read in the data with year as a string variable and then used encode to generate a numeric variable. That's quite the wrong approach, as you have found out: you do not want the string to be mapped to integers 1 up. You need destring for that situation. Now that you have done this, you need decode and then destring or (if the original variable is still present in the dataset) destring.

Note that you should check your data carefully. Why was year imported in this way in the first place? Often this happens when data come from a spreadsheet and people don't check carefully enough for metadata (e.g. header information).

clear
input str4 original 
"1990"
"1991"
"1992"
end 
encode original, gen(year) 

* solution 1 
decode year, gen(year2) 
destring year2, replace 

* solution 2 (better) 
destring original, replace 

list 

     +-------------------------+
     | original   year   year2 |
     |-------------------------|
  1. |     1990   1990    1990 |
  2. |     1991   1991    1991 |
  3. |     1992   1992    1992 |
     +-------------------------+

Also, in Stata, "format" is nothing to do with what is stored, but with what is displayed. See help format. It is, naturally, an overloaded term in computing.