Announcement

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

  • Combining two float variables

    I am trying to combine two float type variables. Currently I have two variables "month" and "year" both are float and when looking at the data set they appear as numbers (ie: month: 4 year:1999) I would like to combine them to read: 4/1999 or 4 1999. Is there a way to do this easily?

  • #2
    gen str newvar=string(month)+"/"+string(year)

    Comment


    • #3
      Rich Goldstein's solution will get you a string variable that looks like what you want. But you won't be able to calculate elapsed time between dates with it, nor readily order them chronologically. Another solution that overcomes these limitations is:

      Code:
      gen newvar = ym(year, month)
      format newvar %tmnn/CCYY
      When you list these variables, they will look like 7/1999. But internally they will be integers coded so that subtraction produces elapsed time in months, and numerical order corresponds to chronological order.

      Comment

      Working...
      X