Announcement

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

  • 'gen var=_n' disrupted time series

    Given below is the original dataset

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 TradingDate long stkcd str13 ShortName byte Filling double ClosePrice
    "2009-01-01" 1 "深发展A" 2 2.983
    "2009-01-02" 1 "深发展A" 2 2.983
    "2009-01-05" 1 "深发展A" 0 3.062
    "2009-01-06" 1 "深发展A" 0 3.248
    "2009-01-07" 1 "深发展A" 0  3.15
    end
    I wanted to generate a counter variable for days given, so I coded
    Code:
    gen var=_n
    after doing this, dates are no longer sorted normally like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 TradingDate long stkcd str13 ShortName byte Filling double(ClosePrice day)
    "2009-09-14" 1 "深发展A" 0  6.93 1
    "2009-03-25" 1 "深发展A" 0 4.982 2
    "2009-06-12" 1 "深发展A" 1 6.306 3
    "2009-03-12" 1 "深发展A" 0 4.767 4
    "2009-04-07" 1 "深发展A" 0  5.25 5
    end
    what should I do to make counter function well like this?
    "2009-01-01" - "1"
    "2009-01-02" - "2"
    ...

  • #2
    Jason:
    I was not able to replicate your problem:
    [CODE][. input str12 TradingDate long stkcd str13 ShortName byte Filling double ClosePrice

    TradingDate stkcd ShortName Filling ClosePrice
    1.
    . "2009-01-01" 1 "深发展A" 2 2.983
    2.
    . "2009-01-02" 1 "深发展A" 2 2.983
    3.
    . "2009-01-05" 1 "深发展A" 0 3.062
    4.
    . "2009-01-06" 1 "深发展A" 0 3.248
    5.
    . "2009-01-07" 1 "深发展A" 0 3.15
    6.
    . end

    . gen var=_n

    . list

    +----------------------------------------------------------+
    | TradingD~e stkcd ShortN~e Filling CloseP~e var |
    |----------------------------------------------------------|
    1. | 2009-01-01 1 深发展A 2 2.983 1 |
    2. | 2009-01-02 1 深发展A 2 2.983 2 |
    3. | 2009-01-05 1 深发展A 0 3.062 3 |
    4. | 2009-01-06 1 深发展A 0 3.248 4 |
    5. | 2009-01-07 1 深发展A 0 3.15 5 |
    +----------------------------------------------------------+

    .
    /CODE]

    That said, the two excerpts that you shared seem to belong to different datasets or different portions of the same dataset.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Jason:
      I was not able to replicate your problem:
      [CODE][. input str12 TradingDate long stkcd str13 ShortName byte Filling double ClosePrice

      TradingDate stkcd ShortName Filling ClosePrice
      1.
      . "2009-01-01" 1 "深发展A" 2 2.983
      2.
      . "2009-01-02" 1 "深发展A" 2 2.983
      3.
      . "2009-01-05" 1 "深发展A" 0 3.062
      4.
      . "2009-01-06" 1 "深发展A" 0 3.248
      5.
      . "2009-01-07" 1 "深发展A" 0 3.15
      6.
      . end

      . gen var=_n

      . list

      +----------------------------------------------------------+
      | TradingD~e stkcd ShortN~e Filling CloseP~e var |
      |----------------------------------------------------------|
      1. | 2009-01-01 1 深发展A 2 2.983 1 |
      2. | 2009-01-02 1 深发展A 2 2.983 2 |
      3. | 2009-01-05 1 深发展A 0 3.062 3 |
      4. | 2009-01-06 1 深发展A 0 3.248 4 |
      5. | 2009-01-07 1 深发展A 0 3.15 5 |
      +----------------------------------------------------------+

      .
      /CODE]

      That said, the two excerpts that you shared seem to belong to different datasets or different portions of the same dataset.
      Hi Sir, Sorry for the problem. The problem is now solved.
      These two exerpts do belong to different portions of the whole dataset. My dataset is too large-scaled and involved many different variables so that i cannot make it to print here.

      Comment

      Working...
      X