Announcement

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

  • Invalid name error

    Hi,

    I'm trying to run some code to perform power calcs. I've taken this from a paper published by a well regarded econ research organisation. When running this, I get a “‘1’ invalid name” error. This relates in some way to the positional arguments. Beyond this I'm stumped.

    Below is a cut down version for illustration. This produces exactly the same error I get when using the more involved programme more relevant to my work.

    *Simple Binary Dependent Power Calcs

    cap prog drop discretepower
    program discretepower
    syntax anything [,alpha(real .05) beta(real .8) pi(real .5)]
    tokenize "‘0’",parse(" ,")
    local rho = ‘1’
    local m = ‘2’
    local p 0 = ‘3’
    local impact = ‘4’
    local z alpha = invnormal(1-(‘alpha’/2))
    local z beta = invnormal(‘beta’)
    local deff = (1+‘rho’*(‘m’-1))
    local p 1 =‘p 0’+‘impact’
    local k =(‘deff’/‘m’)*( (‘p 1’*(1-‘p 1’)/‘pi’)+(‘p 0’*(1-‘p 0’)/(1-‘pi’)) ) *((‘z alpha’+‘z beta’)ˆ2)/‘impact’ˆ2 di
    di "Total number of clusters= " ‘k’
    end

    *Example

    discretepower .05 30 .5 .1

    Grateful in advance for any pointers on where i'm going wrong. I'm using STATA 12.1.

    Will
    Last edited by William Teager; 22 Mar 2021, 12:35.

  • #2
    It looks like you copied this code from a PDF or Word Document or some other program that outsmarted you by replacing correct Stata local macro quotes (` and ') by "smart" quotes (‘ and '). Those smart quotes are not recognized in Stata, so all of your local macro references are messed up. You need to go through and globally replace those "smart" quotes with the correct characters.

    In addition to that, some of these attempted local macro definitions have invalid names that contain blank spaces, e.g. -local z alpha = ...- is going to produce a local macro called z whose contents are the string alpha = ... So you need to fix all of those as well. Once you do that, I think it will run properly. If not, post back with the (partially) fixed code and I'll try to troubleshoot further.

    Comment


    • #3
      Code like this makes me guess that you copied and pasted from a .pdf and the underscores got left behind. The original code I guess said p_0 and p_1 and z_alpha and z_beta. So if that is the issue you need to re-insert all the underscores that got left behind.

      Code:
      local p 0 = ‘3’

      Comment


      • #4
        In addition to Clyde's and Nick's responses, the caret (^) symbol also wasn't recognized by the copy-paste. Making the mentioned changes results in a working program. Strictly speaking, I don't think you need tokenize after the syntax command. A couple of recommendations, you may consider using scalars instead of locals for storing numbers, as they hold more precision. Also returning the number of clusters rounded up is a good idea too.

        Code:
        cap prog drop discretepower
        program discretepower
          syntax anything [,alpha(real .05) beta(real .8) pi(real .5)]
          local rho = `1'
          local m = `2'
          local p0 = `3'
          local impact = `4'
          local z_alpha = invnormal(1-(`alpha'/2))
          local z_beta = invnormal(`beta')
          local deff = (1+`rho'*(`m'-1))
          local p1 =`p0'+`impact'
          local k =(`deff'/`m')*( (`p1'*(1-`p1')/`pi')+(`p0'*(1-`p0')/(1-`pi')) ) *((`z_alpha'+`z_beta')^2)/`impact'^2
          di "Total number of clusters= " `k'
        end
        
        *Example
        discretepower .05 30 .5 .1
        Res:

        Code:
        . discretepower .05 30 .5 .1
        Total number of clusters= 62.817201

        Comment


        • #5
          Thank you all. This has been incredibly helpful. Annoyed to have missed such simple translation errors. I do have an original much longer dofile published alongside this that includes the full code i'm trying to run (sample calcs allowing for interaction with covariates). This gave the same error - the above was just copied for illustration. I'll have a dig into this and see if there are similar issues going on with how the original codebook was prepared for publication.

          Comment

          Working...
          X