Monday, November 19, 2012

Stata Programming

Stata programming
      • di "`2+2'" // ==> N/A
      • local x 2
        • di "`=`x'-2'" // ==> 0
      • local pth "c:\project"
        • di "`pth'\data\" //==> "c:\project\data\"
      • global pt "c:\project"
        • di "$pt\data\" //==> "c:\project\data\"
        • di "${pt}data\" //==> "c:\projectdata\"
      • local a 2+3
      • local b 7
        • display `a'+`b' //==> 10
        • display "`a'+`b'" //==> 2+3+7
        • display "`a'"+"`b'" //==> 2+3+"7" invalid name
        • display "`a'""+""`b'" //==> 2+3+7
      • regress mpg weight
        • local rsqf e(r2)
        • local rsqv = e(r2)
        • di "R-squared_1f=`rsqf'"  //==> R-squared_1f=e(r2)
        • di "R-squared_1v=`rsqv'"  //==> R-squared_1v=.6515312529087511
        • di "R-squared_2f=" `rsqf' //==> R-squared_2f=.65153125
        • di "R-squared_2v=" `rsqv' //==> R-squared_2v=.65153125
        • di "R-squared_3f=" "``rsqf''" //==> R-squared_3f=.6515312529087511
        • di "R-squared_3v=" "``rsqv''" //==> N/A
  • Stata Blog: Programming an estimation command in Stata
      • Example 1: Storing and extracting the result of an extended macro function
        • local count : word count a b c
        • display "count contains `count'" ==> count contains 3
      • Example 2: Using gettoken to store first token only
        • local mylist y x1 x2
        • display "mylist contains `mylist'" ==> mylist contains y x1 x2
        • gettoken first : mylist
        • display "first contains `first'" ==> first contains y
      • Example 3: Using gettoken to store first and remaining tokens
        • gettoken first left: mylist
        • display "first contains `first'" ==> first contains y
        • display "left  contains `left'" ==> left  contains  x1 x2
      • Example 4: Local macro update
        • local p = 1
        • local p = `p' + 3
        • display "p is now `p'" ==> p is now 4
      • Example 5: Local macro update
        • local p = 1
        • local ++p
        • display "p is now `p'" ==> p is now 2

No comments: