Announcement

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

  • How to include conditions in program (.ado file)

    Hi,

    I am very new to writing programs in Stata. My current project is to write an ado-file to get the mean of a variable conditional on a time period (I know this is already possible with built in commands).
    I have the following dataset and want to calculate the mean price for a specified time period.
    Code:
    year price
    2002 20
    2003 21
    2004 19
    2005 18
    2006 21
    2007 22
    2008 20
    2009 21
    so in the end I want to type for example progname price if inrange(year,2004,2008) to get the mean price between 2004 and 2008. (Actually the program is supposed to give me means, 95ci and export everything to excel, but one problem at a time).

    What I cannot figure out is how to get the if-condition into the program.
    The basic version from where I started is:

    Code:
    cap program drop pmean
    program pmean, rclass
            version 15
            syntax varlist [if] [in]
            
            qui sum `1'
            return list
    end
    where in the program do I specify the year(s) and how would you suggest to do it?

    Thank you for your help
    Kind regards

    Magnus

  • #2
    Using viewsource, have a look at someone else's ado-file (including StataCorp's) and you'll virtually always see creation of a temporary "touse" variable (and you'll see how its done, and takes account of if and in too). Then all commands like your summarize one have "if `touse'" appended to them so that the appropriate sample is used

    Comment


    • #3
      Stephen Jenkins thank you, I'll give that a try

      Comment

      Working...
      X