Announcement

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

  • Log function within loops

    Hello Stata experts,

    I am trying to perform a log function within loops, but it gives me errors that I do not know how to fix it. Your help would be much appreciated. Thanks!

    This is the code that I am running:

    Code:
    forvalues i = 6/40 {
        forvalues j = 1/6{
            foreach k in lmn if gestage2==`i' & bmiprepregcat6==`j'{
                foreach l in lsd if gestage2==`i' & bmiprepregcat6==`j'{
                    replace zscore = (log(gwgkg +`c')-`k')/`l' if gestage2==`i' & bmiprepregcat6==1
                }
            }
        }
    }
    This code gives me the r(198) error, but I already created the "gwgkg" as a continuous variable and I do not know why the plus (+) sign got mixed up with the variable name.

    gwgkg+ invalid name
    r(198);
    I tried using parenthesis to make the variable name "gwgkg" and it does not work either.
    Code:
    forvalues i = 6/40 {
        forvalues j = 1/6{
            foreach k in lmn if gestage2==`i' & bmiprepregcat6==`j'{
                foreach l in lsd if gestage2==`i' & bmiprepregcat6==`j'{
                    replace zscore = ((log(gwgkg) +`c')-`k')/`l' if gestage2==`i' & bmiprepregcat6==1
                }
            }
        }
    }
    Now the error is:


    unknown function ()
    r(133);
    Thank you!
    Last edited by Mahmoud Amoli; 08 Nov 2021, 07:12.

  • #2
    The local macro c is not defined.

    Putting an if qualifier on a loop makes no sense to Stata but that has yet to crash your code.
    Last edited by Nick Cox; 08 Nov 2021, 07:19.

    Comment


    • #3
      Thanks for your response dear Nick Cox,

      I defined the local c in the data, but I forgot to put it here:

      Code:
      local c = 5.55
      The original code that I created the loop for looks like this:

      Code:
      replace zscore=(log(gwgkg+`c')-1.870)/0.337 if gestage2==6 & bmiprepregcat6==1
      replace zscore=(log(gwgkg+`c')-1.896)/0.330 if gestage2==7 & bmiprepregcat6==1
      the numbers 1.87 and 1.896 are coming from the "lmn" variable and 0.33 and 0.337 are from the "lsd" variable"

      thanks!

      Comment


      • #4
        The issue is not what you forgot; it's that Stata can't see the definition.

        Why isn't this just


        Code:
        replace zscore = ((log(gwgkg) + 5.55)- lmn)/lsd
        This looks legal
        Code:
          
         forvalues i = 6/40 {     forvalues j = 1/6 {         replace zscore = ((log(gwgkg) + 5.55)-lmn)/lsd if gestage2==`i' & bmiprepregcat6==`j'      } }

        Comment


        • #5
          The reason that I had to define a local instead of simply putting 5.55 is I have multiple values for c such as 5.55 - 9- 12- 18.

          Thank you!

          Comment


          • #6
            I think there is no way that was clear in anything you've said so far. But if c is variable your code needs to adjust to that.

            If you can't work out which way to go now, we need a heap more context about what the goal is.

            Comment


            • #7
              Dear Nick Cox,

              I see! That's ok; I can do the rest manually, not a big deal.

              Thank you so very much; You are great,!

              and btw, I am a great fan of your tutorials

              Comment

              Working...
              X