Announcement

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

  • .ado Programming in Stata without invoking mata: Invalid Name error from within the function

    Stata Status: Beginner

    I have declared a local variable inside my function. And whenever I reference it for a mathematical operation, it gives me the error 'invalid name'. A snippet of the code is given below:

    Code:
    cap program drop ix
    
    program ix, rclass
        version 15.1
        syntax varlist(numeric min=1 max=1), ix(string)
        
        local y `:word 1 of `varlist''
        sum `y'
        .
        .
        .
        tempname nt
        matrix nt  = `ix'
        gen v = y:*nt
    
        .
        .
        .
        //return lists
    end
    I have put a trace and it stops at the gen v line with 'y: invalid name'. I have tried using `y', $y, y. Nothing works.
    Note: The variable y has content, it is not 0. The matrix from ix and y have dimensions that allow the :* operation. nt is not empty.

    Where am I going wrong?
    Last edited by Nilima Pisharody; 23 Apr 2019, 14:43.

  • #2
    The character sequence :* is not a valid operator in Stata. That's Mata.

    Either you have to go into Mata and pull those matrices from Stata into Mata and then use :*, or you can stay in Stata and write some code that will do elementwise multiplication of the matrices by looping over the elements of the matrices. But you can't use :* in Stata.

    Comment


    • #3
      Thank you for replying so promptly!

      However, even when I change the operator, to a '*' and get rid of nt, and just use a constant (say, 2), it shows 'y not found'

      Comment


      • #4
        Stata is telling you that there is no variable named y.

        Earlier in the program you define a local macro y to be the first item in the varlist, and you then summarize the variable represented by y using the command
        Code:
        sum `y'
        Note the left and right single quotes highlighted in red that surround the y. That is what is missing from your generate command - it causes Stata to insert the value that the local macro y was defined to be.

        Comment


        • #5
          Along the lines of William's comment, note that by referring to nt you are creating a permanent object; instead, you want

          Code:
          tempname nt
          ... `nt'
          ​​
          Best
          Daniel

          Comment


          • #6
            Hi, William and Daniel! initially, I thought that was the problem too. Which is why in my question, I clarified it. I have switched between all referencing mechanisms already `y', $y and y. Nothing works.

            Comment


            • #7
              It's time to suggest that you take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], as described in section 12 of the FAQ.

              Section 12.1 is particularly pertinent

              12.1 What to say about your commands and your problem

              Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
              ...
              Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.[/QUOTE]

              The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

              Comment


              • #8
                Let's return to #1. First, if your variable list must be just one variable name, you can use the resulting local directly. That reduces the program to

                Code:
                program ix, rclass    
                version 15.1    
                syntax varlist(numeric min=1 max=1), ix(string)          
                sum `varlist'      
                tempname nt    
                matrix `nt' = `ix'    
                gen v = `varlist' * `nt'      
                //return lists
                end
                Clearly I can't fill in your blanks.

                With the other problems pointed out to you fixed, you still have a big problem. Stata won't let you multiply a variable and a matrix like that.

                I think you best need Mata for what you want to do, contrary to the thread title. Or, old-fashioned but still workable, you could copy your variable to a Stata matrix. Whether the result will fit into a variable naturally depends on the dimensions of the result.
                Last edited by Nick Cox; 25 Apr 2019, 03:52.

                Comment

                Working...
                X