Announcement

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

  • How to exclude . in the count of unique values?

    I'm trying to follow what they have listed on the guide here but they don't include the case where one might want to exclude . in their count of unique values.

  • #2
    use the user-written -distinct- command (by the authors of the FAQ you link to); use -findit- or -search- to find and download; see (after installing)
    Code:
    help distinct

    Comment


    • #3
      There are various way to tackle that, not least specifying if !missing() with a variable.

      Here's another: distinct from the Stata Journal excludes missings by default. Consider this example:

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . distinct rep78
      
      ------------------------------
             |     total   distinct
      -------+----------------------
       rep78 |        69          5
      ------------------------------
      
      . distinct rep78, missing
      
      ------------------------------
             |     total   distinct
      -------+----------------------
       rep78 |        74          6
      ------------------------------
      Incidentally, an update of distinct in press for Stata Journal 20(4) has sorting options, such as
      Code:
       
      . distinct, sort(distinct)
      
      -------------------------------------
                    |     total   distinct
      --------------+----------------------
            foreign |        74          2
              rep78 |        69          5
           headroom |        74          8
               turn |        74         18
              trunk |        74         18
                mpg |        74         21
       displacement |        74         31
         gear_ratio |        74         36
             length |        74         47
             weight |        74         64
               make |        74         74
              price |        74         74
      -------------------------------------
      
      . distinct, sort(distinct descending)
      
      -------------------------------------
                    |     total   distinct
      --------------+----------------------
              price |        74         74
               make |        74         74
             weight |        74         64
             length |        74         47
         gear_ratio |        74         36
       displacement |        74         31
                mpg |        74         21
              trunk |        74         18
               turn |        74         18
           headroom |        74          8
              rep78 |        69          5
            foreign |        74          2
      -------------------------------------

      .



      Comment


      • #4
        How do I store it in a variable? STATA is giving me an error when I try to do that.

        Comment


        • #5
          you don't say what you tried so we can't say what you did incorrectly; I do note that the result is saved (see he help file); I also note that you probably do not really want a variable that is constant for each and every observation so what do you want? it can be saved as a local; if you really want it as a variable:
          Code:
          gen junk=r(ndistinct)
          replace "junk" with whatever you want your new variable to be called

          save as a local:
          Code:
          local junk=r(ndistinct)

          Comment


          • #6
            Originally posted by Rich Goldstein View Post
            you don't say what you tried so we can't say what you did incorrectly


            ​​​​​​​This is the error I'm getting in the debugging log:

            Code:
            - gen nvals = `this_variable'(ndistinct)
            = gen nvals = rsex(ndistinct)
            unknown function rsex()

            Comment


            • #7
              That is not what Rich Goldstein is suggesting. I think we need to know more of what you are trying to do to give further advice.

              Comment

              Working...
              X