Announcement

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

  • Renaming year variable

    Hi All. Sorry for such a silly question, because I have read the help section in stata on this and I am continuing to get an error message.

    I am working on 1:1 merge of two county-level data sets by year and fips variables but getting more an append. I thought it was because of an issue with fips variable (state and county codes), but I think it might be because one data set the year variable is in 4-digits and the other data set the variable is in 2 digits. I wanted to change the 4-digits into 2-digits. In a previous post from Clyde Schechter, he suggested very very helpful information on renaming year variable. But I am getting a continuous error message.

    Code:
    rename *20# *#
    Code:
    variable *20?* not found // error message I am receiving
    Not sure what to do. Here is a little sample of my variables

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long fips_code int year
    1001 2013
    1001 2014
    1001 2016
    1001 2015
    1001 2012
    1003 2014
    1003 2012
    1003 2016
    1003 2015
    1003 2013
    end
    I am thinking once I can fix the year variable to two digits then the 1:1 merge between county-level data will merge accurately.

    Thanks

    Rene
    Using Stata 12.1 on Mac OS Catalina

  • #2
    Code:
    replace year = mod(year, 2000)

    Comment


    • #3
      The name of your variable is year and its values are numbers like 2013. Changing the name has nothing do with changing the values.

      A better idea would just be to change the display format.


      Code:
      . display %tyY 2013
      13
      Subtracting 2000 -- or extracting the last two digits with mod() as in @Joseph Coveney's excellent answer -- may look a benign change for your data example -- until you have to work out why 08 displays as 8 and you get puzzling results trying to compare values in 99 and in 00.

      Comment


      • #4
        Just wondering, "adding" two digits in the other dataset (and adjusting for the millenium as well as century).
        Best regards,

        Marcos

        Comment


        • #5
          Indeed, making the year variables comparable by adding 2000, or whatever, to one is more important. My post in #3 is perhaps tangential to the original question.

          Comment


          • #6
            Thank you so much Joseph Coveney and Nick Cox. It worked great and my merge worked perfectly.

            Comment

            Working...
            X