Announcement

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

  • failure to run program

    Dear Stata Forum members,

    I made a program like this:

    cap program drop sampletps
    program define sampletps
    version 14.2

    syntax [anything] , p(real .5) N(real) ntps(real) sigma(real .001) ci(real .95)
    local z = invnormal((1-`ci')/2)
    local sampledpt = (`p'*(1-`p'))/((`sigma'^2/`z'^2)+((`p'*(1-`p'))/`N'))
    local dpttps = `N'/`ntps'
    local sampletps = `sampledpt'/`dpttps'
    di "Samples DPT needed = " `sampledpt'
    di "Samples Polling Stations needed = " `sampletps'
    end

    * test
    sampletps, p(.5) N(192000000) ntps(801838) sigma(.001) ci(.99)

    however, when I test it, it failed. I got the message like this


    invalid syntax
    r(197);

    can somebody help me what's wrong with this code? Thank you


    Best regards,

    Afri

  • #2
    You have two problems.

    1) Because you do not specify default values for your options n and ntps, they are required options, and because you do specify default values for your other options, they are optional options, and your syntax command should read
    Code:
    syntax [anything] , n(real) ntps(real) [p(real .5) sigma(real .001) ci(real .95)]
    2) The syntax command uses capitalization to indicate the minimum abbreviation and the options are required to be given as lowercase in the command that calls the program. Thus N(192000000) should be n(192000000).=
    Code:
    . capture program drop sampletps
    
    . program define sampletps
      1. version 14.2
      2. 
    . syntax [anything] , N(real) ntps(real) [p(real .5) sigma(real .001) ci(real .95)]
      3. display "N is `N'"
      4. display "n is `n'"
      5. end
    
    . 
    . * test
    
    . sampletps, N(192000000) ntps(801838) p(.5) sigma(.001) ci(.99)
    option n() required
    r(198);
    
    . 
    . sampletps, n(192000000) ntps(801838) p(.5) sigma(.001) ci(.99)
    N is 
    n is 192000000
    
    .

    Comment


    • #3
      Thank you very much, William. Yes, it works perfectly according to your suggestion.

      Comment

      Working...
      X