Announcement

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

  • Defining Program Stata Help

    Hi

    2 Questions -

    1) I was wondering if anyone had advice on how to learn to define programs in Stata. Are there any resources you could point me towards?
    2) I want to define a program on Stata but I am not sure if it is possible so please forgive me if what I am asking is nonsense. I would like to create a Stata program that would help provide descriptive statistics with just one line of code. For example

    cap program drop descstat
    program define descstat, rclass
    version 13.0
    syntax tab(varlist) codebook(varlist) sum(varlist) table(varlist)
    tab1 `tab', m
    sum `sum'
    codebook `codebook'
    table `table'
    end

    (Let's assume we use sysuse auto.dta) The command I would like to enter is

    descstat tab(foreign make) sum(price-weight) codebook(rep78) table(foreign rep78)


    The code above does not work (obviously), but I inserted it to give you an idea of what I am trying to achieve. I want to create a program that would provide the tab of the specific variables i list within the "(varlist)", sum of the variables and codebook of the variables that I specify.....Is this possible?

    Thanks,

  • #2
    You were very close. The main problem is that options need to be preceded by a comma, both when running your descat command and in the syntax command. Your secondary problem is that all of your options are required because they are not surrounded by square brackets to denote them as optional.

    This works for me.
    Code:
    cap program drop descstat
    program define descstat, rclass
    version 13.0
    syntax [, tab(varlist) codebook(varlist) sum(varlist) table(varlist)]
    if "`tab'"!="" tab1 `tab', m
    if "`sum'"!="" sum `sum'
    if "`codebook'"!="" codebook `codebook'
    if "`table'"!="" table `table'
    end
    
    sysuse auto, clear
    descstat, tab(foreign make) sum(price-weight) codebook(rep78) table(foreign rep78)
    descstat, codebook(foreign)
    As for increasing your knowledge of defining programs, have you reviewed Chapter 18 Programming Stata in the Stata User's Guide PDF included in your Stata installation and accessible from Stata's Help menu. Stata supplies exceptionally good documentation that amply repays the time spent studying it.

    Comment


    • #3
      William Lisowski - Thank you so much! This is very helpful and thank you for taking time to explain it. Will certainly study Chapter 18 more closely. Thanks again

      Comment

      Working...
      X