Announcement

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

  • Handling weekly data

    Hello statalist members,
    with reference to my last post on https://www.statalist.org/forums/for...gression/page3
    i want to use weekly data, for that very purpose i need date format based on year and week, and i also need the total number of weeks in a year,i also want to take leads and lags of my variable of interest, and for that, i have to tsset or xtset my data.to avoid the problem of
    repeated time values within panel
    i have created a new variable dategroup

    Code:
    egen dategroup=group( year week)
    so that i can tsset my data on the base of this variable and firm-ID (code) for further work as a week variable.But i need to use a proper way as compare to dategroup variable to deal with it.
    could you please guide me how can i format my date variable trdwnt , so that i can use it for my further work, and also to calculate the total number of trading weeks in a year.
    In below example week 2002-07 and 2002-08 are missings, so i want to deal with such like missing values as well.


    My dataset example is

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long code str7 trdwnt str10 opndt float wopnprc str10 clsdt float wclsprc int year byte week float dategroup
    2 "2002-01" "2002-01-04" 13.01 "2002-01-04" 12.99 2002  1   1
    2 "2002-02" "2002-01-07"  12.9 "2002-01-11" 12.31 2002  2   2
    2 "2002-03" "2002-01-14"  12.2 "2002-01-18" 11.28 2002  3   3
    2 "2002-04" "2002-01-21" 11.27 "2002-01-25"    12 2002  4   4
    2 "2002-05" "2002-01-28" 11.76 "2002-02-01" 12.49 2002  5   5
    2 "2002-06" "2002-02-04" 12.45 "2002-02-08"  12.5 2002  6   6
    2 "2002-09" "2002-02-25" 12.91 "2002-03-01" 12.47 2002  9   7
    2 "2002-10" "2002-03-04" 12.45 "2002-03-08"  13.4 2002 10   8
    2 "2002-11" "2002-03-11" 13.39 "2002-03-15" 13.01 2002 11   9
    2 "2002-12" "2002-03-18"  13.4 "2002-03-22" 13.12 2002 12  10
    2 "2002-13" "2002-03-25"  13.1 "2002-03-29" 12.68 2002 13  11
    2 "2002-14" "2002-04-01"  12.6 "2002-04-05" 12.66 2002 14  12
    2 "2002-15" "2002-04-08" 12.61 "2002-04-12" 12.72 2002 15  13
    2 "2002-16" "2002-04-15" 12.72 "2002-04-19" 12.47 2002 16  14
    2 "2002-17" "2002-04-22" 12.35 "2002-04-26" 12.33 2002 17  15
    2 "2002-18" "2002-04-29" 12.33 "2002-04-30" 12.42 2002 18  16
    2 "2002-19" "2002-05-08"  12.4 "2002-05-10" 12.14 2002 19  17
    2 "2002-20" "2002-05-13" 12.06 "2002-05-17" 11.48 2002 20  18
    2 "2002-21" "2002-05-20" 11.51 "2002-05-24" 11.82 2002 21  19
    2 "2002-22" "2002-05-27" 11.94 "2002-05-31"  11.3 2002 22  20
    2 "2002-23" "2002-06-03"  11.2 "2002-06-07" 11.42 2002 23  21
    2 "2002-24" "2002-06-10" 11.42 "2002-06-11" 11.43 2002 24  22
    2 "2002-25" "2002-06-18"  11.5 "2002-06-21"  11.9 2002 25  23
    2 "2002-26" "2002-06-24" 13.09 "2002-06-28" 13.21 2002 26  24
    2 "2002-27" "2002-07-01" 13.27 "2002-07-05" 12.83 2002 27  25
    2 "2002-28" "2002-07-08" 12.85 "2002-07-12" 12.59 2002 28  26
    2 "2002-29" "2002-07-15" 12.55 "2002-07-19" 12.52 2002 29  27
    2 "2002-30" "2002-07-22" 12.51 "2002-07-26" 12.01 2002 30  28
    2 "2002-31" "2002-07-29" 12.05 "2002-08-02" 12.13 2002 31  29
    2 "2002-32" "2002-08-05" 12.18 "2002-08-09"  11.9 2002 32  30
    2 "2002-33" "2002-08-12" 11.87 "2002-08-16" 11.79 2002 33  31
    2 "2002-34" "2002-08-19" 11.78 "2002-08-23" 12.14 2002 34  32
    2 "2002-35" "2002-08-26"  12.1 "2002-08-30" 11.87 2002 35  33
    2 "2002-36" "2002-09-02" 11.87 "2002-09-06" 11.72 2002 36  34
    2 "2002-37" "2002-09-09"  11.7 "2002-09-13" 11.58 2002 37  35
    2 "2002-38" "2002-09-16" 11.57 "2002-09-20" 11.55 2002 38  36
    2 "2002-39" "2002-09-23" 11.53 "2002-09-27" 11.43 2002 39  37
    2 "2002-41" "2002-10-08" 11.38 "2002-10-11"  11.1 2002 41  38
    2 "2002-42" "2002-10-14" 11.19 "2002-10-18"  11.1 2002 42  39
    2 "2002-43" "2002-10-21"  11.1 "2002-10-25"  11.1 2002 43  40
    2 "2002-44" "2002-10-28"    11 "2002-11-01" 11.05 2002 44  41
    2 "2002-45" "2002-11-04" 11.05 "2002-11-08" 11.24 2002 45  42
    2 "2002-46" "2002-11-11"  11.2 "2002-11-15" 10.09 2002 46  43
    2 "2002-47" "2002-11-18" 10.09 "2002-11-22" 10.22 2002 47  44
    2 "2002-48" "2002-11-25" 10.21 "2002-11-29" 10.13 2002 48  45
    2 "2002-49" "2002-12-02" 10.13 "2002-12-06"  9.86 2002 49  46
    2 "2002-50" "2002-12-09"   9.8 "2002-12-13"  9.77 2002 50  47
    2 "2002-51" "2002-12-16"  9.78 "2002-12-20"  9.94 2002 51  48
    2 "2002-52" "2002-12-23"  9.91 "2002-12-27"  9.79 2002 52  49
    2 "2002-53" "2002-12-30"   9.7 "2002-12-31"  9.65 2002 53  50
    2 "2003-01" "2003-01-02"  9.48 "2003-01-03"  9.09 2003  1  51
    2 "2003-02" "2003-01-06"  9.08 "2003-01-10"  9.54 2003  2  52
    2 "2003-03" "2003-01-13"   9.5 "2003-01-17" 10.16 2003  3  53
    2 "2003-04" "2003-01-20" 10.16 "2003-01-24"  9.96 2003  4  54
    2 "2003-05" "2003-01-27"  9.96 "2003-01-29" 10.07 2003  5  55
    2 "2003-07" "2003-02-10"  10.1 "2003-02-14" 10.26 2003  7  56
    2 "2003-08" "2003-02-17" 10.25 "2003-02-21"  9.94 2003  8  57
    2 "2003-09" "2003-02-24"  9.94 "2003-02-28" 10.33 2003  9  58
    2 "2003-10" "2003-03-03" 10.35 "2003-03-07"  10.9 2003 10  59
    2 "2003-11" "2003-03-10"  10.9 "2003-03-14" 11.31 2003 11  60
    2 "2003-12" "2003-03-17" 11.32 "2003-03-21" 11.42 2003 12  61
    2 "2003-13" "2003-03-24" 11.46 "2003-03-28" 11.55 2003 13  62
    2 "2003-14" "2003-03-31" 11.58 "2003-04-04" 11.98 2003 14  63
    2 "2003-15" "2003-04-07"    12 "2003-04-11" 12.76 2003 15  64
    2 "2003-16" "2003-04-14" 12.78 "2003-04-18" 13.89 2003 16  65
    2 "2003-17" "2003-04-21" 13.79 "2003-04-25" 12.21 2003 17  66
    2 "2003-18" "2003-04-28" 12.01 "2003-04-30" 12.84 2003 18  67
    2 "2003-20" "2003-05-12" 12.92 "2003-05-16" 13.88 2003 20  68
    2 "2003-21" "2003-05-19" 13.93 "2003-05-23"  6.79 2003 21  69
    2 "2003-22" "2003-05-26"  6.79 "2003-05-30"  7.13 2003 22  70
    2 "2003-23" "2003-06-02"   7.1 "2003-06-06"  6.78 2003 23  71
    2 "2003-24" "2003-06-09"  6.78 "2003-06-13"  6.82 2003 24  72
    2 "2003-25" "2003-06-16"  6.79 "2003-06-20"  6.41 2003 25  73
    2 "2003-26" "2003-06-23"   6.4 "2003-06-27"  5.83 2003 26  74
    2 "2003-27" "2003-06-30"   5.7 "2003-07-04"     6 2003 27  75
    2 "2003-28" "2003-07-07"     6 "2003-07-11"  6.21 2003 28  76
    2 "2003-29" "2003-07-14"  6.21 "2003-07-18"  6.25 2003 29  77
    2 "2003-30" "2003-07-21"   6.2 "2003-07-25"  6.16 2003 30  78
    2 "2003-31" "2003-07-28"  6.16 "2003-08-01"  6.39 2003 31  79
    2 "2003-32" "2003-08-04"  6.38 "2003-08-08"  6.49 2003 32  80
    2 "2003-33" "2003-08-11"  6.48 "2003-08-15"   6.5 2003 33  81
    2 "2003-34" "2003-08-18"  6.52 "2003-08-22"  6.64 2003 34  82
    2 "2003-35" "2003-08-25"   6.6 "2003-08-29"  6.46 2003 35  83
    2 "2003-36" "2003-09-01"  6.45 "2003-09-05"  6.34 2003 36  84
    2 "2003-37" "2003-09-08"  6.34 "2003-09-12"  6.04 2003 37  85
    2 "2003-38" "2003-09-15"  6.02 "2003-09-19"  5.81 2003 38  86
    2 "2003-39" "2003-09-22"  5.83 "2003-09-26"  5.91 2003 39  87
    2 "2003-40" "2003-09-29"   5.9 "2003-09-30"  5.86 2003 40  88
    2 "2003-41" "2003-10-08"  5.88 "2003-10-10"  6.05 2003 41  89
    2 "2003-42" "2003-10-13"  6.09 "2003-10-17"   5.9 2003 42  90
    2 "2003-43" "2003-10-20"  5.88 "2003-10-24"  6.06 2003 43  91
    2 "2003-44" "2003-10-28"  6.02 "2003-10-31"  5.94 2003 44  92
    2 "2003-45" "2003-11-03"  5.89 "2003-11-07"  5.95 2003 45  93
    2 "2003-46" "2003-11-10"  5.95 "2003-11-14"  5.81 2003 46  94
    2 "2003-47" "2003-11-17"  5.81 "2003-11-21"  5.87 2003 47  95
    2 "2003-48" "2003-11-24"  5.87 "2003-11-28"  5.91 2003 48  96
    2 "2003-49" "2003-12-01"  5.91 "2003-12-05"  6.02 2003 49  97
    2 "2003-50" "2003-12-08"  6.01 "2003-12-12"  6.11 2003 50  98
    2 "2003-51" "2003-12-15"  6.12 "2003-12-19"     6 2003 51  99
    2 "2003-52" "2003-12-22"     6 "2003-12-26"  6.53 2003 52 100
    end
    label var code "Stkcd" 
    label var trdwnt "Trading Week '" 
    label var opndt "Weekly Opening Date '" 
    label var wopnprc "Weekly Opening Price '" 
    label var clsdt "Weekly Closing Date '" 
    label var wclsprc "Weekly Closing Price '" 
    label var dategroup "group(year week)"
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 1755974 observations
    Use the count() option to list more


    Best regards,
    Last edited by Ayub UOM; 16 Sep 2019, 22:07.

  • #2
    I don't understand all of what you want to do, but this helps a lot

    This example makes it clear that as guessed in the linked thread, the data provider is not using Stata's week definition, Sufficient refutation is the occurrence of week 53, which never occurs with Stata's definition. So, Stata's week functions and week formats can't do anything but produce a mess. People find this difficult to believe, but it's usually true.

    The occurrence of gaps in your data (better called gaps, or absent values, than missing values) makes your dategroup variable problematic, Another solution is suggested below. It seems that apart from holidays and gaps in your data weeks start on Mondays and end on Fridays, So, as already suggested, you can index weeks by the Mondays that (in the data or implicitly) start them.

    Here's some code

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long code str7 trdwnt str10 opndt float wopnprc str10 clsdt float wclsprc int year byte week float dategroup
    2 "2002-01" "2002-01-04" 13.01 "2002-01-04" 12.99 2002  1   1
    2 "2002-02" "2002-01-07"  12.9 "2002-01-11" 12.31 2002  2   2
    2 "2002-03" "2002-01-14"  12.2 "2002-01-18" 11.28 2002  3   3
    2 "2002-04" "2002-01-21" 11.27 "2002-01-25"    12 2002  4   4
    2 "2002-05" "2002-01-28" 11.76 "2002-02-01" 12.49 2002  5   5
    2 "2002-06" "2002-02-04" 12.45 "2002-02-08"  12.5 2002  6   6
    2 "2002-09" "2002-02-25" 12.91 "2002-03-01" 12.47 2002  9   7
    2 "2002-10" "2002-03-04" 12.45 "2002-03-08"  13.4 2002 10   8
    end 
    
    foreach v in opndt clsdt { 
        gen `v'_num = daily(`v', "YMD")
        format `v'_num %td
        drop `v'
        rename `v'_num `v'
    }
    
    list, sep(0)
    
    drop week dategroup 
    
    gen week_start = opndt - dow(opndt) + 1 
    gen week_end = week_start + 4 
    format week* %td 
    
    list , sep(0)
    
    tsset code week_start, delta(7)
    and here are listings for that sub-example

    Code:
    . list, sep(0)
    
         +-------------------------------------------------------------------------------------+
         | code    trdwnt   wopnprc   wclsprc   year   week   dategr~p       opndt       clsdt |
         |-------------------------------------------------------------------------------------|
      1. |    2   2002-01     13.01     12.99   2002      1          1   04jan2002   04jan2002 |
      2. |    2   2002-02      12.9     12.31   2002      2          2   07jan2002   11jan2002 |
      3. |    2   2002-03      12.2     11.28   2002      3          3   14jan2002   18jan2002 |
      4. |    2   2002-04     11.27        12   2002      4          4   21jan2002   25jan2002 |
      5. |    2   2002-05     11.76     12.49   2002      5          5   28jan2002   01feb2002 |
      6. |    2   2002-06     12.45      12.5   2002      6          6   04feb2002   08feb2002 |
      7. |    2   2002-09     12.91     12.47   2002      9          7   25feb2002   01mar2002 |
      8. |    2   2002-10     12.45      13.4   2002     10          8   04mar2002   08mar2002 |
         +-------------------------------------------------------------------------------------+
    
    . list , sep(0)
    
         +-------------------------------------------------------------------------------------------+
         | code    trdwnt   wopnprc   wclsprc   year       opndt       clsdt   week_st~t    week_end |
         |-------------------------------------------------------------------------------------------|
      1. |    2   2002-01     13.01     12.99   2002   04jan2002   04jan2002   31dec2001   04jan2002 |
      2. |    2   2002-02      12.9     12.31   2002   07jan2002   11jan2002   07jan2002   11jan2002 |
      3. |    2   2002-03      12.2     11.28   2002   14jan2002   18jan2002   14jan2002   18jan2002 |
      4. |    2   2002-04     11.27        12   2002   21jan2002   25jan2002   21jan2002   25jan2002 |
      5. |    2   2002-05     11.76     12.49   2002   28jan2002   01feb2002   28jan2002   01feb2002 |
      6. |    2   2002-06     12.45      12.5   2002   04feb2002   08feb2002   04feb2002   08feb2002 |
      7. |    2   2002-09     12.91     12.47   2002   25feb2002   01mar2002   25feb2002   01mar2002 |
      8. |    2   2002-10     12.45      13.4   2002   04mar2002   08mar2002   04mar2002   08mar2002 |
         +-------------------------------------------------------------------------------------------+

    Comment


    • #3
      Thank you sir for your time and help, it is working well, but i am still facing one problem
      . tsset code week_start, delta(7)
      repeated time values within panel
      so how can i deal with this issue of duplicates value, although code is same but trdwnt is different and also week_start is same so i am getting repeated time values message ?
      and one more thing how to generate the number of weeks per year,

      i have tags some duplicate values based on code and week_st~t week_end
      example is given below
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long code str7 trdwnt int year float(opndt clsdt week_start week_end)
       2 "2002-53" 2002 15704 15705 15704 15708
       2 "2003-01" 2003 15707 15708 15704 15708
       2 "2003-53" 2003 16068 16070 16068 16072
       2 "2004-01" 2004 16072 16072 16068 16072
       2 "2013-53" 2013 19722 19723 19722 19726
       2 "2014-01" 2014 19725 19726 19722 19726
       4 "2002-53" 2002 15704 15705 15704 15708
       4 "2003-01" 2003 15707 15708 15704 15708
       4 "2003-53" 2003 16068 16070 16068 16072
       4 "2004-01" 2004 16072 16072 16068 16072
       4 "2012-53" 2012 19358 19358 19358 19362
       4 "2013-01" 2013 19362 19362 19358 19362
       4 "2013-53" 2013 19722 19723 19722 19726
       4 "2014-01" 2014 19725 19726 19722 19726
       5 "2002-53" 2002 15704 15705 15704 15708
       5 "2003-01" 2003 15707 15708 15704 15708
       5 "2003-53" 2003 16068 16070 16068 16072
       5 "2004-01" 2004 16072 16072 16068 16072
       5 "2012-53" 2012 19358 19358 19358 19362
       5 "2013-01" 2013 19362 19362 19358 19362
       5 "2013-53" 2013 19722 19723 19722 19726
       5 "2014-01" 2014 19725 19726 19722 19726
       6 "2002-53" 2002 15704 15705 15704 15708
       6 "2003-01" 2003 15707 15708 15704 15708
       6 "2003-53" 2003 16068 16070 16068 16072
       6 "2004-01" 2004 16072 16072 16068 16072
       6 "2012-53" 2012 19358 19358 19358 19362
       6 "2013-01" 2013 19362 19362 19358 19362
       6 "2013-53" 2013 19722 19723 19722 19726
       6 "2014-01" 2014 19725 19726 19722 19726
       7 "2002-53" 2002 15704 15705 15704 15708
       7 "2003-01" 2003 15707 15708 15704 15708
       7 "2003-53" 2003 16068 16070 16068 16072
       7 "2004-01" 2004 16072 16072 16068 16072
       7 "2012-53" 2012 19358 19358 19358 19362
       7 "2013-01" 2013 19362 19362 19358 19362
       7 "2013-53" 2013 19722 19723 19722 19726
       7 "2014-01" 2014 19725 19726 19722 19726
       8 "2002-53" 2002 15704 15705 15704 15708
       8 "2003-01" 2003 15707 15708 15704 15708
       8 "2003-53" 2003 16068 16070 16068 16072
       8 "2004-01" 2004 16072 16072 16068 16072
       8 "2012-53" 2012 19358 19358 19358 19362
       8 "2013-01" 2013 19362 19362 19358 19362
       8 "2013-53" 2013 19722 19723 19722 19726
       8 "2014-01" 2014 19725 19726 19722 19726
       9 "2002-53" 2002 15704 15705 15704 15708
       9 "2003-01" 2003 15707 15708 15704 15708
       9 "2003-53" 2003 16068 16070 16068 16072
       9 "2004-01" 2004 16072 16072 16068 16072
       9 "2012-53" 2012 19358 19358 19358 19362
       9 "2013-01" 2013 19362 19362 19358 19362
       9 "2013-53" 2013 19722 19723 19722 19726
       9 "2014-01" 2014 19725 19726 19722 19726
      10 "2002-53" 2002 15704 15705 15704 15708
      10 "2003-01" 2003 15707 15708 15704 15708
      10 "2003-53" 2003 16068 16070 16068 16072
      10 "2004-01" 2004 16072 16072 16068 16072
      10 "2013-53" 2013 19722 19723 19722 19726
      10 "2014-01" 2014 19725 19726 19722 19726
      11 "2002-53" 2002 15704 15705 15704 15708
      11 "2003-01" 2003 15707 15708 15704 15708
      11 "2003-53" 2003 16068 16070 16068 16072
      11 "2004-01" 2004 16072 16072 16068 16072
      11 "2012-53" 2012 19358 19358 19358 19362
      11 "2013-01" 2013 19362 19362 19358 19362
      11 "2013-53" 2013 19722 19723 19722 19726
      11 "2014-01" 2014 19725 19726 19722 19726
      12 "2002-53" 2002 15704 15705 15704 15708
      12 "2003-01" 2003 15707 15708 15704 15708
      12 "2003-53" 2003 16068 16070 16068 16072
      12 "2004-01" 2004 16072 16072 16068 16072
      12 "2012-53" 2012 19358 19358 19358 19362
      12 "2013-01" 2013 19362 19362 19358 19362
      12 "2013-53" 2013 19722 19723 19722 19726
      12 "2014-01" 2014 19725 19726 19722 19726
      13 "2002-53" 2002 15704 15705 15704 15708
      13 "2003-01" 2003 15707 15708 15704 15708
      13 "2003-53" 2003 16068 16070 16068 16072
      13 "2004-01" 2004 16072 16072 16068 16072
      14 "2002-53" 2002 15705 15705 15704 15708
      14 "2003-01" 2003 15707 15708 15704 15708
      14 "2003-53" 2003 16068 16070 16068 16072
      14 "2004-01" 2004 16072 16072 16068 16072
      14 "2012-53" 2012 19358 19358 19358 19362
      14 "2013-01" 2013 19362 19362 19358 19362
      14 "2013-53" 2013 19722 19723 19722 19726
      14 "2014-01" 2014 19725 19726 19722 19726
      16 "2002-53" 2002 15704 15705 15704 15708
      16 "2003-01" 2003 15707 15708 15704 15708
      16 "2003-53" 2003 16068 16070 16068 16072
      16 "2004-01" 2004 16072 16072 16068 16072
      16 "2012-53" 2012 19358 19358 19358 19362
      16 "2013-01" 2013 19362 19362 19358 19362
      16 "2013-53" 2013 19722 19723 19722 19726
      16 "2014-01" 2014 19725 19726 19722 19726
      17 "2002-53" 2002 15704 15705 15704 15708
      17 "2003-01" 2003 15707 15708 15704 15708
      17 "2003-53" 2003 16068 16070 16068 16072
      17 "2004-01" 2004 16072 16072 16068 16072
      end
      format %td opndt
      format %td clsdt
      format %td week_start
      format %td week_end
      label var code "Stkcd" 
      label var trdwnt "Trading Week '"
      sorry for the below format its jsut few values from the above
      Code:
      code    trdwnt    year    opndt    clsdt    week_start    week_end
      2    2002-53    2002    30dec2002    31dec2002    30dec2002    03jan2003
      2    2003-01    2003    02jan2003    03jan2003    30dec2002    03jan2003
      2    2003-53    2003    29dec2003    31dec2003    29dec2003    02jan2004
      2    2004-01    2004    02jan2004    02jan2004    29dec2003    02jan2004
      2    2013-53    2013    30dec2013    31dec2013    30dec2013    03jan2014
      2    2014-01    2014    02jan2014    03jan2014    30dec2013    03jan2014
      4    2002-53    2002    30dec2002    31dec2002    30dec2002    03jan2003
      4    2003-01    2003    02jan2003    03jan2003    30dec2002    03jan2003
      4    2003-53    2003    29dec2003    31dec2003    29dec2003    02jan2004
      4    2004-01    2004    02jan2004    02jan2004    29dec2003    02jan2004
      4    2012-53    2012    31dec2012    31dec2012    31dec2012    04jan2013
      4    2013-01    2013    04jan2013    04jan2013    31dec2012    04jan2013
      4    2013-53    2013    30dec2013    31dec2013    30dec2013    03jan2014
      4    2014-01    2014    02jan2014    03jan2014    30dec2013    03jan2014
      5    2002-53    2002    30dec2002    31dec2002    30dec2002    03jan2003
      5    2003-01    2003    02jan2003    03jan2003    30dec2002    03jan2003
      5    2003-53    2003    29dec2003    31dec2003    29dec2003    02jan2004
      Thank you

      Comment


      • #4
        So your data provider is splitting weeks at year end. Here is some code that merges duplicates (not tested!).

        Code:
        foreach v in opndt clsdt {      
            gen `v'_num = daily(`v', "YMD")    
            format `v'_num %td    
            drop `v'    
            rename `v'_num `v'
        }  
        
        list, sep(0)  
        drop week dategroup  
        gen week_start = opndt - dow(opndt) + 1  
        gen week_end = week_start + 4  
        format week* %td  
        
        bysort code week_start (opndt) : replace opndt = opndt[1]  
        by code week_start : replace wopnprc = wopnprc[1]  
        by code week_start : replace clsdt = clsdt[_N]  
        by code week_start : replace wclsprc = wclsprc[_N]  
        
        duplicates drop code week_start, force  
        
        tsset code week_start, delta(7)
        The number of weeks for which you have data is just


        Code:
        bysort code year : gen count = _N
        Last edited by Nick Cox; 17 Sep 2019, 05:29.

        Comment


        • #5
          Nick Cox
          Thank you so much sir for helping me out, its worked very well,everything is ok, appreciate your every reply because of your know-how about the stata ,you picked a very genuine issue of 53 weeks mostly in my data set and then taking a naive approach to solve this issue.
          now i can use week_start as my date variable for regression for weekly data and year for yearly data
          Code:
          rangestat(reg) y x1 x2 x3 ,by( code ) interval( week_start  . .)
          best regards.

          Comment


          • #6
            Hello Nick Cox
            I would like to ask One more question about the lead and lag values, week number 2002-07 and 2002-08 are missing in year 2002, so it creates problems in lead and lags, how can we solve it?
            6. | 2 2002-06 12.45 12.5 2002 6 6 04feb2002 08feb2002 | 7. | 2 2002-09 12.91 12.47 2002 9 7 25feb2002 01mar2002 | best regards

            Comment


            • #7
              Whenever there are gaps in panel time series, some leading and lagged values will be missing. This is just what it is. There are no solutions but living with it or interpolation.

              Comment


              • #8
                ok sir, thank you so much.
                sir i want to calculate Residuals return from market model based on my weekly data, sir i just want to confirm from you that in my case i will use by(code year) in blow command or any other time format.
                CODE] runby one_regression, by(code year) status [/CODE]
                thank you sir in advance for your patient and cooperation.


                Comment


                • #9
                  Thanks for the thanks, but if the question is: Is this right for what I want to do? then all I can say is that it's not obviously wrong. Much depends on what is in the program one_regression.

                  Comment


                  • #10
                    actually i want to prepare my data for replicating these commands ,this is an example from other thread ,i just present here to prepare my time variable for my runby command.
                    Code:
                     input str8 code int yearcount str10 weekdate float(vnindex2 vnindex1 vnindex vnindex3 vnindex4) str12 weekret
                    "KPF" 2017 "12/27/2017" -.024829805 .031005034 .015557268 .03770208 .031747766 "0.33050207"  
                    "KPF" 2017 "12/20/2017" -.0047374 -.024829805 .031005034 .015557268 .03770208 "0.333144447"
                     "KPF" 2017 "12/13/2017" .020671364 -.0047374 -.024829805 .031005034 .015557268 "0.32613822"
                     "KPF" 2017 "12/6/2017" .05517995 .020671364 -.0047374 -.024829805 .031005034 "0.157917163"  
                    "KPF" 2017 "11/29/2017" .026277276 .05517995 .020671364 -.0047374 -.024829805 "0.334461681"  
                    "KPF" 2017 "11/22/2017" .0199606 .026277276 .05517995 .020671364 -.0047374 "0.067236945"
                     "KPF" 2017 "11/15/2017" .014366209 .0199606 .026277276 .05517995 .020671364 "-0.065477929"
                    "KPF" 2017 "11/8/2017" .003581748 .014366209 .0199606 .026277276 .05517995 "0.117562989"  
                    "KPF" 2017 "11/1/2017" .016775994 .003581748 .014366209 .0199606 .026277276 "-0.00197824"

                    i want to follow these below commands, although the clean up part is covered with your cooperation in my data


                    Code:
                     //    CLEAN UP THE DATA A BIT
                    assert !missing(real(weekret))
                    destring weekret, replace
                    gen date = daily(weekdate, "MDY")
                    assert !missing(date)
                    assert yearcount == year(date)  
                    
                    
                    capture progrm drop one_regression
                    program define one_regression    
                         capture regress weekret vnindex*    
                         if c(rc) == 0 {    // SUCCESSFUL REGRESSION      
                                gen n_obs = e(N)        
                                gen r2 = e(r2)        
                                foreach v of varlist vnindex* {          
                                   gen b_`v' = _b[`v']            
                                  gen se_`v' = _se[`v']    
                          }        
                         predict residual, resid      
                         exit 0    
                     }    
                    else if inlist(c(rc), 2000, 2001) {        
                        gen comment = "No or insufficient observations, error `c(rc)'"      
                       exit 0     }    
                    else {    // UNEXPECTED ERROR      
                            gen comment = "Unexpected regression error, error `c(rc)'"        
                            exit c(rc)  
                       }
                     end  
                    ​​​​​​​
                    runby one_regression, by(code yearcount) status
                    sir i hope it will be clear now.
                    Last edited by Ayub UOM; 18 Sep 2019, 03:34.

                    Comment


                    • #11
                      Hello statalist members,
                      i want to generate my date variable in the below format for monthly data set,

                      Code:
                       
                       gen ym = mofd(date) format ym %tm
                      although i tried the above code but my variable is string so i did not generated it.,therefore need your guidence please
                      below is my dataset
                      ----------------------- copy starting from the next line -----------------------
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input long code str7 trdmnt byte(opndt clsdt)
                      2 "2003-10"  8 31
                      2 "2003-09"  1 30
                      2 "2003-06"  2 30
                      2 "2003-07"  1 31
                      2 "2003-11"  3 28
                      2 "2003-03"  3 31
                      2 "2003-08"  1 29
                      2 "2003-02" 10 28
                      2 "2003-01"  2 29
                      2 "2003-04"  1 30
                      2 "2003-05" 12 30
                      2 "2003-12"  1 31
                      2 "2004-02"  2 27
                      2 "2004-03"  1 31
                      2 "2004-06"  1 30
                      2 "2004-12"  1 31
                      2 "2004-07"  1 30
                      2 "2004-10"  8 29
                      2 "2004-09"  1 30
                      2 "2004-01"  2 30
                      2 "2004-11"  1 30
                      2 "2004-05" 10 31
                      2 "2004-08"  2 31
                      2 "2004-04"  1 30
                      2 "2005-03"  1 31
                      2 "2005-10" 20 31
                      2 "2005-09"  1 30
                      2 "2005-11"  1  4
                      2 "2005-08"  1 31
                      2 "2005-06"  1 30
                      2 "2005-04"  1 28
                      2 "2005-02"  1 28
                      2 "2005-01"  4 31
                      2 "2005-12"  2 30
                      2 "2005-05"  9 31
                      2 "2005-07"  1 29
                      2 "2006-04"  3 28
                      2 "2006-07"  3 31
                      2 "2006-11"  1 30
                      2 "2006-05"  8 31
                      2 "2006-10"  9 31
                      2 "2006-01"  4 25
                      2 "2006-08"  1 31
                      2 "2006-03"  1 31
                      2 "2006-06"  1 30
                      2 "2006-09"  1 29
                      2 "2006-12"  1 29
                      2 "2006-02"  6 28
                      2 "2007-06"  1 29
                      2 "2007-04"  2 30
                      2 "2007-09"  3 28
                      2 "2007-07"  2 31
                      2 "2007-11"  1 30
                      2 "2007-08"  1 31
                      2 "2007-02"  1 28
                      2 "2007-10"  8 31
                      2 "2007-05"  8 31
                      2 "2007-03"  1 30
                      2 "2007-12"  3 28
                      2 "2007-01"  4 31
                      2 "2008-05"  5 30
                      2 "2008-02"  1 29
                      2 "2008-08"  1 29
                      2 "2008-06"  2 30
                      2 "2008-12"  1 31
                      2 "2008-07"  1 31
                      2 "2008-09"  1 26
                      2 "2008-10"  6 31
                      2 "2008-04"  1 30
                      2 "2008-03"  3 31
                      2 "2008-01"  2 31
                      2 "2008-11"  3 28
                      2 "2009-03"  2 31
                      2 "2009-12"  1 31
                      2 "2009-07"  1 31
                      2 "2009-02"  2 27
                      2 "2009-06"  1 30
                      2 "2009-10"  9 30
                      2 "2009-08"  3 31
                      2 "2009-01"  5 23
                      2 "2009-05"  4 27
                      2 "2009-09"  1 30
                      2 "2009-04"  1 30
                      2 "2009-11"  2 30
                      2 "2010-08"  2 31
                      2 "2010-11"  1 30
                      2 "2010-02"  1 26
                      2 "2010-12"  1 31
                      2 "2010-10"  8 29
                      2 "2010-03"  1 31
                      2 "2010-07"  1 30
                      2 "2010-04"  1 30
                      2 "2010-01"  4 29
                      2 "2010-09"  1 30
                      2 "2010-06"  1 30
                      2 "2010-05"  4 31
                      2 "2011-11"  1 30
                      2 "2011-07"  1 29
                      2 "2011-08"  1 31
                      2 "2011-04"  1 29
                      end
                      best regards

                      Comment


                      • #12
                        You want

                        Code:
                        gen ym = monthly(trdmnt, "YM" )
                        format ym %tm

                        Comment


                        • #13
                          Andrew Musau thank you so much for your time, it is very easy and working easily as compare to the below command, because it requires to first split and destring variable and then generates mdatec
                          Code:
                          gen mdatec = ym(year, month) 
                          format mdatec %tm
                          ​​​​​​​so yours one is very good for my case.thanks again

                          Comment

                          Working...
                          X