Announcement

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

  • Command for counting

    Hello,

    I am new in this forum.
    I have a question, I would like to create a new variable which will count the days ( for in stance equal to 1 for the first day that I have etc) but because I have data from many companies I have the same date many time so I would like for instance for 01/01/2017 , which I have it 10 times, the variable to take the value 1, for the 0201/2012 equal to 2 and etc. Is that possible? Is there any command that I could use? If my question is not clear enough please let me know and I will try t explain it better. Thank you in advance!

  • #2
    In general, if you want people to show you code, you should show an example of your data, using the -dataex- command. Please see FAQ #12 for details on how to use -dataex-. Otherwise you are asking people to write code for data that exists in their imaginations, and which may not resemble what you actually have.

    That said, I'll try to guess. I assume you have a variable, named date, which is a numeric Stata internal format date variable. This is critical: if it is a string variable, the code I show will not work and you will have to first convert it.

    Code:
    by date, sort: gen date_seq = sum(date != date[_n-1])

    Comment


    • #3
      Dear Clyde,

      Thank you ery much for your response. The date is not a string variable. I tried the given code but I got the value 1 for all the dates. What I want is when I have companies with the same date to count that date as one and to have as many days as my data are, I am examining the period 2008-2015.

      [CODE]
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str8 Country str15 Code double delta_log_price long date int year float(datenum count1 date_seq)
      "Spain" "ES0176406066(P)" -4.51085950651685 19498 2013 3143910 0 1
      "Spain" "ES0128461037(P)" -2.305252946955238 19894 2014 3784712 0 1
      "Italy" "IT0005187940(P)" -2.2072749131897207 20079 2014 4077853 0 1
      "Spain" "ES0181222011(P)" -2.0149030205422647 20172 2015 4225906 0 1
      "Italy" "IT0004818297(P)" -1.9073390519180036 19158 2012 2597588 0 1
      "Greece" "GRS149001000(P)" -1.791759469228055 18682 2011 1828075 0 1
      "Spain" "ES0181480114(P)" -1.6511921847816873 18191 2009 1046104 0 1
      "Italy" "IT0005187940(P)" -1.5040773967762742 20076 2014 4074883 0 1
      "Spain" "ES0172821037(P)" -1.482098490057499 18179 2009 1028621 0 1
      "Portugal" "PTSCB0AM0001(P)" -1.4816045409242153 19277 2012 2789737 0 1
      "Greece" "GRS065003006(P)" -1.4271163556401456 18464 2010 1480778 0 1
      "Portugal" "PTCDU0AE0003(P)" -1.330346287187358 18408 2010 1389524 0 1
      "Portugal" "PTRED0AP0010(P)" -1.286210902562908 20104 2015 4121640 0 1
      "Portugal" "PTFEN0AP0006(P)" -1.2809338454620642 18318 2010 1244210 0 1
      "Italy" "PTLIT0AE0005(P)" -1.2671642647383075 19491 2013 3132214 0 1
      "Ireland" "IE0003073255(P)" -1.2367626271489267 18925 2011 2219528 0 1
      "Portugal" "PTGPA0AP0007(P)" -1.2343575409526524 19198 2012 2661085 0 1
      "Spain" "ES0109260531(P)" -1.223940768703466 20256 2015 4360383 0 1
      "Italy" "PTLIT0AE0005(P)" -1.2211182112809245 19247 2012 2738175 0 1
      "Portugal" "PTCUR0AP0000(P)" -1.205222023726368 19478 2013 3112955 0 1
      "Italy" "IT0003056386(P)" -1.165009874612094 20436 2015 4650701 0 1
      "Ireland" "IE00B66ML191(P)" -1.139434283188365 18941 2011 2246865 0 1
      "Portugal" "PTCUR0AP0000(P)" -1.1394342831883648 18486 2010 1514173 0 1
      "Portugal" "PTGPA0AP0007(P)" -1.1332614240812837 19219 2012 2694872 0 1
      "Portugal" "PTCDU0AE0003(P)" -1.126648720891393 18443 2010 1445170 0 1
      "Italy" "IT0005187940(P)" -1.123305172261155 19236 2012 2722797 0 1

      Comment


      • #4
        Code:
        by Country (date), sort: gen date_seq = sum(date != date[_n-1])

        Comment


        • #5
          Dear Clyde,

          Thank you very much.

          Comment

          Working...
          X