Announcement

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

  • Destring Time variable & Removing hyphen (i.e. "-") in the date

    Dear Statalisters

    My dataset contains share price indices of different countries. Before declaring the dataset as panel data, I need to convert the date from string to numeric variable.

    Here's an example of my dataset, where value is the share price index & location is the country's abbreviation,:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 ïlocation str1 frequency str7 time float value
    "AUS" "Q" "1958-Q1" 2.997673
    "AUS" "Q" "1958-Q2" 3.020671
    "AUS" "Q" "1958-Q3" 3.175909
    "AUS" "Q" "1958-Q4" 3.183096
    "AUS" "Q" "1959-Q1" 3.367082
    "AUS" "Q" "1959-Q2" 3.579815
    "AUS" "Q" "1959-Q3" 4.058466
    "AUS" "Q" "1959-Q4" 4.420688
    "AUS" "Q" "1960-Q1" 4.712477
    "AUS" "Q" "1960-Q2" 4.802314
    "AUS" "Q" "1960-Q3"  4.95899
    "AUS" "Q" "1960-Q4" 4.316477
    "AUS" "Q" "1961-Q1" 4.161958
    "AUS" "Q" "1961-Q2" 4.414219
    "AUS" "Q" "1961-Q3" 4.410626
    "AUS" "Q" "1961-Q4" 4.321508
    "AUS" "Q" "1962-Q1" 4.553646
    "AUS" "Q" "1962-Q2" 4.393377
    "AUS" "Q" "1962-Q3" 4.228796
    "AUS" "Q" "1962-Q4" 4.247482
    "AUS" "Q" "1963-Q1" 4.431468
    "AUS" "Q" "1963-Q2" 4.583113
    "AUS" "Q" "1963-Q3" 4.991331
    "AUS" "Q" "1963-Q4" 5.072543
    "AUS" "Q" "1964-Q1" 5.432609
    "AUS" "Q" "1964-Q2" 5.372957
    "AUS" "Q" "1964-Q3" 5.418954
    "AUS" "Q" "1964-Q4" 5.226344
    "AUS" "Q" "1965-Q1" 5.027266
    "AUS" "Q" "1965-Q2" 4.629827
    "AUS" "Q" "1965-Q3" 4.581675
    "AUS" "Q" "1965-Q4" 4.594612
    "AUS" "Q" "1966-Q1" 4.698103
    "AUS" "Q" "1966-Q2"  4.68373
    "AUS" "Q" "1966-Q3"  4.72182
    "AUS" "Q" "1966-Q4" 4.612579
    "AUS" "Q" "1967-Q1" 4.787941
    "AUS" "Q" "1967-Q2" 4.971926
    "AUS" "Q" "1967-Q3" 5.733742
    "AUS" "Q" "1967-Q4"  6.45028
    "AUS" "Q" "1968-Q1" 6.751413
    "AUS" "Q" "1968-Q2" 8.032125
    "AUS" "Q" "1968-Q3" 8.683981
    "AUS" "Q" "1968-Q4" 8.046499
    "AUS" "Q" "1969-Q1"   9.0038
    "AUS" "Q" "1969-Q2" 8.770944
    "AUS" "Q" "1969-Q3" 8.194551
    "AUS" "Q" "1969-Q4"  8.69045
    "AUS" "Q" "1970-Q1" 9.079982
    "AUS" "Q" "1970-Q2" 7.971756
    "AUS" "Q" "1970-Q3" 8.364881
    "AUS" "Q" "1970-Q4" 7.794238
    "AUS" "Q" "1971-Q1" 7.254498
    "AUS" "Q" "1971-Q2" 7.241562
    "AUS" "Q" "1971-Q3"  6.85778
    "AUS" "Q" "1971-Q4" 6.383441
    "AUS" "Q" "1972-Q1"  7.61672
    "AUS" "Q" "1972-Q2" 8.545273
    "AUS" "Q" "1972-Q3" 8.498558
    "AUS" "Q" "1972-Q4" 8.681107
    "AUS" "Q" "1973-Q1" 8.676076
    "AUS" "Q" "1973-Q2" 8.033563
    "AUS" "Q" "1973-Q3" 7.736742
    "AUS" "Q" "1973-Q4" 6.727696
    "AUS" "Q" "1974-Q1" 7.329243
    "AUS" "Q" "1974-Q2" 6.690324
    "AUS" "Q" "1974-Q3" 4.729007
    "AUS" "Q" "1974-Q4" 4.379004
    "AUS" "Q" "1975-Q1" 5.067513
    "AUS" "Q" "1975-Q2"  5.23928
    "AUS" "Q" "1975-Q3"  5.36577
    "AUS" "Q" "1975-Q4" 6.080871
    "AUS" "Q" "1976-Q1" 6.688886
    "AUS" "Q" "1976-Q2" 6.742788
    "AUS" "Q" "1976-Q3" 7.209221
    "AUS" "Q" "1976-Q4" 6.283543
    "AUS" "Q" "1977-Q1" 6.346788
    "AUS" "Q" "1977-Q2" 6.558803
    "AUS" "Q" "1977-Q3" 6.420814
    "AUS" "Q" "1977-Q4" 6.497714
    "AUS" "Q" "1978-Q1" 6.678106
    "AUS" "Q" "1978-Q2" 7.039609
    "AUS" "Q" "1978-Q3" 7.715181
    "AUS" "Q" "1978-Q4" 7.830891
    "AUS" "Q" "1979-Q1" 8.418064
    "AUS" "Q" "1979-Q2" 8.463342
    "AUS" "Q" "1979-Q3" 9.110167
    "AUS" "Q" "1979-Q4" 10.19468
    "AUS" "Q" "1980-Q1" 12.24583
    "AUS" "Q" "1980-Q2" 12.59368
    "AUS" "Q" "1980-Q3"  14.3092
    "AUS" "Q" "1980-Q4" 15.65747
    "AUS" "Q" "1981-Q1" 14.49678
    "AUS" "Q" "1981-Q2" 15.23129
    "AUS" "Q" "1981-Q3" 13.20744
    "AUS" "Q" "1981-Q4" 12.49953
    "AUS" "Q" "1982-Q1" 10.79407
    "AUS" "Q" "1982-Q2" 10.64745
    "AUS" "Q" "1982-Q3" 10.42897
    "AUS" "Q" "1982-Q4" 10.59643
    end

    Since the date contains a hyphen(-) between year & quarter, I've tried the following code to remove the hyphen:
    Code:
    destring time, ignore("-") replace
    But it wasn't successful. Does anyone know how to remove the hyphen for the dates?

    Thank you very much.

  • #2
    Converting the variable to a Stata numeric date for setting up panel data is not just a matter of eliminating the hyphen. Nor will -destring- help you for this in any way.

    Also, to set this up as panel data, you will need a numeric variable corresponding to ïlocation; strings are not allowed as panel identifiers. So all in all:

    Code:
    gen qdate = quarterly(time, "YQ")
    assert !missing(qdate)
    format qdate %tq
    
    encode ïlocation, gen(location)
    
    xtset location qdate
    If working with panel data will be part of your regular work, it behooves you to become familiar with how dates are handled in Stata. It's pretty complicated. But you need to understand how the numeric representation of dates works, how they can be formatted to look like human-readable dates in output displays and the browser even though they are really just numbers, and how you can convert from strings to numeric dates or from one type of numeric date to another. To learn this, run -help datetime- and then click on the [View complete PDF manual entry] link near the top of that page. Read that entire section of the manual. It's a long read, with a lot of material. You won't remember it all. But you will at least get the concepts, and in the future you will probably recall which function(s) might be helpful in any given circumstance. Then you can refer to the specific help files,l or back to the PDF manual for the details.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Converting the variable to a Stata numeric date for setting up panel data is not just a matter of eliminating the hyphen. Nor will -destring- help you for this in any way.

      Also, to set this up as panel data, you will need a numeric variable corresponding to ïlocation; strings are not allowed as panel identifiers. So all in all:

      Code:
      gen qdate = quarterly(time, "YQ")
      assert !missing(qdate)
      format qdate %tq
      
      encode ïlocation, gen(location)
      
      xtset location qdate
      If working with panel data will be part of your regular work, it behooves you to become familiar with how dates are handled in Stata. It's pretty complicated. But you need to understand how the numeric representation of dates works, how they can be formatted to look like human-readable dates in output displays and the browser even though they are really just numbers, and how you can convert from strings to numeric dates or from one type of numeric date to another. To learn this, run -help datetime- and then click on the [View complete PDF manual entry] link near the top of that page. Read that entire section of the manual. It's a long read, with a lot of material. You won't remember it all. But you will at least get the concepts, and in the future you will probably recall which function(s) might be helpful in any given circumstance. Then you can refer to the specific help files,l or back to the PDF manual for the details.
      Thank you very much for your help,Clyde. You're right that I have to be familiar with how dates are handled in Stata,as I'm working on my dissertation & there're lots of instances that I need to deal with Panel data. I'll read the entire manual to at least grasp the concepts.

      Comment

      Working...
      X