Announcement

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

  • Generating variables based conditionally with log of zero-value variable

    Hi experts,
    Today I try to find a way to generate a variable called logTAT1.

    Normally, logTAT1= ln(TAT)
    When the value of TAT-0, I will use the min value of TAT instead

    So, my code is:

    Code:
          gen logTAT1=.
          replace logTAT1 = ln(TAT)
          replace logTAT1 = ln(min(TAT)) if TAT=0
    However, it resutls in an error.

    Code:
    invalid syntax
    r(198);
    
    end of do-file
    
    r(198);

    Could you please help me to sort it out? Thank you.

  • #2
    Phuc:
    Code:
     
     replace logTAT1 = ln(min(TAT)) if TAT==0
    will probably do the trick.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Phuc:
      Code:
      replace logTAT1 = ln(min(TAT)) if TAT==0
      will probably do the trick.
      Thanks Carlo Lazzaro , I followed your guide but there are problems happened

      Click image for larger version

Name:	1.png
Views:	1
Size:	7.9 KB
ID:	1696021

      Comment


      • #4
        The function min() does not work like that. It works row-wise over two or more arguments, not column-wise over the elements of a variable or vector or matrix.

        But even if min() did work like that the result would still be zero whenever zeros are present. Your code does not ignore the zeros.

        What you trying to do, I think is

        Code:
        su TAT if TAT > 0
        
        gen wanted = cond(TAT == 0, log(r(min)), log(TAT))
        but that is still a bad idea without substantive grounds. What some people do is use log(half smallest positive value) if a value is zero, but that is a fudge too.
        Last edited by Nick Cox; 05 Jan 2023, 05:05.

        Comment


        • #5
          Hi Nick Cox ,

          Thank you for your help, the idea coming from a paper from a good journal (Kolias,2011). In his paper, he use the smallest value replacing zero when getting log

          Best regards,
          Phuc

          Comment


          • #6
            I have no cats. So, if your desired variable is log of number of cats possessed, you are going to assume that I have 1 cat.. No thanks!

            More importantly, there are many better ideas. See e.g.. the concurrent thread https://www.statalist.org/forums/for...-be-dealt-with

            Comment


            • #7
              Originally posted by Nick Cox View Post
              I have no cats. So, if your desired variable is log of number of cats possessed, you are going to assume that I have 1 cat.. No thanks!

              More importantly, there are many better ideas. See e.g.. the concurrent thread https://www.statalist.org/forums/for...-be-dealt-with
              Thank you Mr.Cox. I understand your example and had a look at the discussion link. I also saw the approach using ln(1+x) has been done in various papers in finance major, especially when calculating non-negative variable e.g., firm age
              Last edited by Phuc Nguyen; 05 Jan 2023, 05:17.

              Comment

              Working...
              X