Announcement

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

  • Tab and Levelsof not giving same result

    What are the possible reasons that the following not give the same result? (tab appears to be evaluating the way I expect. levelsof is returning more values than appear to meet the conditions).

    tab location if _merge==1 & Datevar<date("20141001", "YMD")
    levelsof location if _merge==1 & Datevar<date("20141001", "YMD")

  • #2
    Please re-run these commands, as well as -des location-, and then show us both the echoed commands and Stata's responses by copy-pasting directly from the Stata Results window into a code block on this forum.

    See the FAQ for how to create a code block on this forum, if you do not already know.

    Please do not retype anything: do it using copy and paste so we can be sure we are looking exactly at the commands you used and exactly at the output Stata generated.

    Comment


    • #3
      Clyde,

      Thanks for the response. Below is an example as you have requested. After some trial and error and fiddling with different syntax, it appears that -levelsof- doesn't like the date("","") function, but does respond properly when the condition is provided using the STATA number for the date or the d(ddmmmyyyy) function. What is strange is that both d(01oct2014) and date("20141001", "YMD") evaluate to the same number, and -tab- is able to understand them, but levelsof is having an issue with the particular function -date()-, while it does understand -d()-

      For example:
      levelsof location if _merge==1 & Datevar<19997

      and

      levelsof location if _merge==1 & Datevar<d(01oct2014)

      both provide the same output as:

      tab location if _merge==1 & Datevar<date("20141001", "YMD")

      but

      levelsof location if _merge==1 & Datevar<date("20141001", "YMD")

      returns a different set of location values compared to all of the above.

      Code:
      .
      tab location if _merge==1 & Datevar<date("20141001", "YMD")
      
      Localizació |
                n |      Freq.     Percent        Cum.
      ------------+-----------------------------------
         xxvv1916 |          1       25.00       25.00
         xxvv1917 |          1       25.00       50.00
         xxvv1918 |          1       25.00       75.00
         xxvv1919 |          1       25.00      100.00
      ------------+-----------------------------------
            Total |          4      100.00
      
      . levelsof location if _merge==1 & Datevar<date("20141001", "YMD")
      `"eet1705"' `"eet1706"' `"vvh3503"' `"vvh3504"' `"vvh3505"' `"vvh3506"' `"vvh3507"' `"w2701"' `"w2702"' `"w2703"' `"w2704"' `"wr28
      > 01"' `"xxvv1916"' `"xxvv1917"' `"xxvv1918"' `"xxvv1919"'
      
      . levelsof location if _merge==1 & Datevar<d(01oct2014)
      `"xxvv1916"' `"xxvv1917"' `"xxvv1918"' `"xxvv1919"'
      
      . di date("20141001", "YMD")
      19997
      
      . di d(01oct2014)
      19997
      
      . levelsof location if _merge==1 & Datevar<19997
      `"xxvv1916"' `"xxvv1917"' `"xxvv1918"' `"xxvv1919"'

      Comment


      • #4
        Code:
        des location
        
                      storage  display     value
        variable name   type   format      label      variable label
        ---------------------------------------------------------------------------------------------------------------------------------
        location        str9   %9s                    Localización

        Comment


        • #5
          Very interesting! I don't have your data set, but I was able to replicate this odd behavior using a different data set I have lying around.

          You should send your code, output, and data to Stata Technical Support: this is almost surely a bug that they will want to fix.

          Comment


          • #6
            Very interesting indeed. I think the problem is due to version control in this case. Back in version 9, Stata's date() function would not process a date string without delimiters:

            Code:
            . dis c(stata_version)
            9.2
            
            . dis date("20141001", "ymd")
            .
            
            . dis date("2014-10-01", "ymd")
            19997
            The levelsof program sets the command interpreter to version 9 and the if condition is interpreted using version 9 syntax. Here's a little demonstration, run on Stata 14, that shows that this is not specific to levelsof:

            Code:
            clear
            program drop _all
            
            program testit9
                
                version 9
                syntax [if] [in]
            
                marksample touse, strok
                
                count if `touse'
                
            end
            
            program testit9b
                
                version 9
                syntax [if] [in]
            
                dis `"`if'"'
                count `if'
                
            end
            
            program testit10
                
                version 10
                syntax [if] [in]
            
                marksample touse, strok
                
                count if `touse'
                
            end
            
            
            set obs 10
            gen location = "xxvv19" + string(_n)
            gen Datevar = 19992 + _n
            format %d Datevar
            list
            
            testit9  if Datevar < date("20141001", "YMD")
            testit9b  if Datevar < date("20141001", "YMD")
            testit10  if Datevar < date("20141001", "YMD")
            
            version 9: dis date("20141001", "YMD")
            version 9: dis date("2014-10-01", "YMD")

            Comment


            • #7
              Robert, I'm impressed!

              I was intrigued enough to spend some time playing with the code and trying to identify the problem in -levelsof-, and being completely mystified by its behavior. It never occurred to me to pay attention to version control!

              Comment

              Working...
              X