Announcement

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

  • Combine two vars without adding them

    I'm looking to do something that seems fairly straightforward, but has been giving me difficulty: I simply want to combine two float vars into one number but not add/multiply/divide/do any arithmetic to them in the process.

    One var is actually a month var and the other is a year var. I want to generate a new year-month var that just looks like: 201407 (that would be 2014 as the year and 07/July as the month). Thanks much.

  • #2
    gen monthyear = year*100 + month
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you, Maarten.

      Comment


      • #4
        However, much depends on why you want this. In particular, monthly dates that jump from say 201712 to 201801 are useless as time variables for all but cosmetic purposes. In each year the 1 gap of 89 as well as the 11 of 1 will be taken literally (meaning numerically) by Stata, including tsset and xtset and everything that depends on their settings.

        This example

        Code:
        . di ym(2018, 5)
        700
        
        . di %tmCYN ym(2018, 5)
        201805
        implies that you are likely to be better off with a monthly date variable that you can format as you like:

        Code:
        gen mdate = ym(year, month) 
        format mdate %tmCYN


        Comment


        • #5
          Right, this is actually for purposes of analyzing changes in other variables over time--the request I received on my end is somewhat unusual, which is part of why I had trouble figuring out how to code it, since I've never had to do it before.

          The request was for the date variables to be able to be used as numeric, so July 2015 (201507) is really treated as the number 201,507, which is smaller than (and earlier in time than) October 2017 (201710), or the number 201,710. Odd way to analyze, I know.

          Comment

          Working...
          X