Announcement

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

  • Keep last digit of a variable

    Hi all,

    I have an (independent) variable 'yearquarter' in my data that looks like "2001q3" and so on.

    After performing regression, I have to generate a graph with the Price as dependent variable on the Y-axis and Yearquarter on the X-axis using "margin and marginsplot".
    But in order to do so, I want to only keep the last digit of the variable yearquarter, so in this case the "3" of 2001q3.

    How can I only keep this last digit?


    Thanks!!

  • #2
    Assuming yearquarter is stored as string:
    Code:
    gen byte q=quarter(quarterly(yearquarter,"YQ"))
    or
    Code:
    gen byte q=real(ustrright(yearquarter,1))

    Assuming it's stored as integer with format %tq:
    Code:
    gen byte q=quarter(yearquarter)

    Stata has many functions to manipulate date variables. Have a look at the help.

    Jean-Claude Arbaut
    Last edited by Jean-Claude Arbaut; 04 May 2019, 07:48.

    Comment


    • #3
      Hi Jean-Claude,

      FYI: I used variables month (HHMOVM:1-12) and year (HHMOVE:1999-2011) to generate a variable yearquarter.

      Code:

      gen yearquarter = qofd(dofm(ym(hhmove, hhmovm)))
      format yearquarter %tq

      The variable yearquarter is stored with format %tq .
      When I use the code you proposed, I do get a variable q with numbers (2,.. 3) but it somehow doesn't correspond with the correct yearquarter date.
      For instance, q gives a value 2 for yearquarter=q3.
      Do you know why not the correct quarter is appointed to the new variable q?

      Thanks!

      Comment


      • #4
        Sorry, my bad. quarter takes a td date, not a tq date. So it should be:

        Code:
        gen byte q=quarter(dofq(yearquarter))

        Jean-Claude Arbaut
        Last edited by Jean-Claude Arbaut; 04 May 2019, 10:51.

        Comment


        • #5
          That helped, thanks a lot!

          Comment

          Working...
          X