I need help with prog

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Hello, I need help with programming on stata any help?
Expert Solution
Step 1

Answer:

Stata programming is not difficult since it mainly involves the use of Stata commands that you already use. The trick to Stata programming is to use the appropriate commands in the right sequence. Of course, this is the trick to any kind of programming.

There are two kinds of files that are used in Stata programming, do-files and ado-files. Do-files are run from the command line using the do command, for example,

do hsbcheck hsberr

Ado-files, on the other hand, work like ordinary Stata commands by just using the file name in the command line, for example,

ttest write, by(female)

In fact, many of the built-in Stata commands are just ado-files, like the ttest command shown above. You can look at the source code for the ado commands using the viewsource command, for example,

viewsource ttest.ado

You can also use viewsource with your do-files.

viewsource hsbcheck.do

Programs and ado-files are the main methods by which Stata code is condensed and generalized.

The program command sets up the code environment for writing a program into memory.

The syntax command parses inputs into a program as macros that can be used within the scope of that program execution.

The tempvar, tempfile, and tempname commands all create objects that can be used within the scope of program execution to avoid any conflict with arbitrary data structures.

The program command

The program command defines the scope of a Stata program inside a do-file or ado-file. When a program command block is executed, Stata stores (until the end of the session) the sequence of commands written inside the block and assigns them to the command name used in the program command. Using program drop before the block will ensure that the command space is available. For example, we might write the following program in an ordinary do-file:

cap prog drop autoreg

prog def autoreg

  reg price mpg i.foreign

end

After executing this command block we could run:

sysuse auto.dta , clear

autoreg

If we did this, Stata would output:

. autoreg

 

      Source |       SS                   df       MS      Number of obs   =        74

-------------+----------------------------------   F(2, 71)        =     14.07

       Model |   180261702         2      90130850.8   Prob > F        =    0.0000

    Residual |   454803695        71     6405685.84   R-squared       =    0.2838

-------------+----------------------------------   Adj R-squared   =    0.2637

       Total |   635065396          73       8699525.97   Root MSE        =    2530.9

 

------------------------------------------------------------------------------

       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]

-------------+----------------------------------------------------------------

         mpg |  -294.1955   55.69172    -5.28   0.000    -405.2417   -183.1494

             |

     foreign |

    Foreign  |   1767.292    700.158     2.52   0.014     371.2169    3163.368

       _cons |   11905.42   1158.634    10.28   0.000     9595.164    14215.67

All this is to say is that Stata has taken the command reg price mpg i.foreign and will execute it whenever autoreg is run as if it were an ordinary command.

The syntax command

The syntax command takes a program block and allows its inputs to be customized based on the context it is being executed in. The syntax command enables all the main features of Stata that appear in ordinary commands, including input lists  if and in restrictions, using targets, = applications, weights, and options

.For now, we will take a simple tour of how syntax creates an adaptive command.

First, let's add simple syntax allowing the user to select the variables and observations they want to include. We might write:

cap prog drop levelslist

prog def levelslist

syntax anything [if]

preserve

  // Implement [if]

  marksample touse

  qui keep if `touse' == 1

 

  // Main program loops

  foreach var of varlist `anything'

{

    qui levelsof `var' , local(levels)

    di " "

    di "Levels of `var': `: var label `var''"

    foreach word in `levels'

{

      di "  `word'"

    }

  }

End

The temp commands

Stata has a set of temp commands that can be used to store information temporarily. This functionality

Categories:

Coding Practices

Stata Coding Practices

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY