Announcement

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

  • looping over dates with formating

    I have a bunch of dates reflecting date of joining (DOJ). I want to do a bunch of calculations for each DOJ. Before getting into the calculations I want to display the date through which I am looping through.

    qui: levelsof DOJ, local(levels)
    foreach l of local levels{
    di "`l'"
    ...
    }

    The code above works, but it displays dates as integers. Is there any way to convert that into intelligible dates. I tried

    qui: levelsof DOE, local(levels)
    foreach l of local levels{
    format `l' %tdMonth dd, CCYY
    di "`l'"
    ...
    }

    but i get errors (see below) and I am not sure how to work around this without generating a new variable converting it into a date and then dropping it before exiting the foreach loop - that sounds inefficient. Thank you for any advise. I feel like I am missing something obvious here.

    . qui: levelsof DOJ, local(levels)

    . foreach l of local levels{
    2. format `l' %td
    3. di "`l'"
    4. }
    8174 invalid name
    r(198);

  • #2
    in your format statement, you have the %td in the wrong place try
    Code:
    format %td `l'

    Comment


    • #3
      Thank you Rich. Fixed that. However, there seems to be something else the issue. I still get an error. here is some play code:

      set obs 10
      generate DOJ = floor((mdy(12,31,2006)-mdy(12,1,1980)+1)*runiform() + mdy(12,1,1980))
      format %td DOJ

      qui: levelsof DOJ, local(levels)
      foreach l of local levels{
      format %td `l'
      di "`l'"

      }

      Comment


      • #4
        You cannot assign a format to a local macro, or its contents, to be remembered or used later. What you can do -- seems similar, but from Stata's point of view is quite different -- is indicate to display what format it should be using.

        So this will work:

        Code:
        clear 
        set obs 10
        generate DOJ = floor((mdy(12,31,2006)-mdy(12,1,1980)+1)*runiform() + mdy(12,1,1980))
        format %td DOJ 
        
        qui: levelsof DOJ, local(levels)
        foreach l of local levels{
            di %td `l'
        } 
        
        05jul1982
        02oct1983
        28jun1984
        26may1995
        20jun1995
        09sep1996
        18jan1997
        10sep1997
        06oct1998
        25aug2003
        But the levelsof and the loop are quite unnecessary here. If you want a display of dates in sort order, you could just use tabulate

        Code:
        clear 
        set obs 10
        generate DOJ = floor((mdy(12,31,2006)-mdy(12,1,1980)+1)*runiform() + mdy(12,1,1980))
        format %td DOJ 
        tabulate DOJ
        Instead of tabulate, I would use groups (SSC) instead for a clean display.

        Code:
        * to use you must first install: 
        * ssc inst groups 
        groups DOJ, show(none) sep(0)
        
          +-----------+
          |       DOJ |
          |-----------|
          | 05jul1982 |
          | 02oct1983 |
          | 28jun1984 |
          | 26may1995 |
          | 20jun1995 |
          | 09sep1996 |
          | 18jan1997 |
          | 10sep1997 |
          | 06oct1998 |
          | 25aug2003 |
          +-----------+

        Comment


        • #5
          Thank you for this very elegant fix with the display command. Also the pointer to the groups command that I will explore more thoroughly. The display option is really good for me as I can also use this to debug the next part of my code that does some date based accounting calculations. Thank you!

          Comment


          • #6
            Just to add somewhat to Nick's point that you cannot apply a format to a local macro, it is important to also remember the distinction between what -display- does and what -list- does. If you have a variable such as DOJ that already has a date format applied to it, -display- will ignore that format if you ask it to display some value of DOJ. All formatting of -display- results has to be explicitly given in the -display- command itself, even if the material being -display-ed is a variable with formatting. By contrast, list respects the formatting of the variables you give it. See this for examples:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input int datevar float float_var
            20693   .3488717
            20694   .2668857
                0   .1366463
            20766 .028556867
            end
            format %td datevar
            format %4.3f float_var
            
            forvalues i = 1/4 {
                display datevar[`i'], float_var[`i']
            }
            
            list datevar float_var
            Last edited by Clyde Schechter; 27 Aug 2016, 09:27. Reason: Correct typos in code

            Comment


            • #7
              Appreciate the point Clyde. Due to Nick and you I learned something new about Stata today after many years of use.

              Comment

              Working...
              X