Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: first program


From   "Rodrigo A. Alfaro" <[email protected]>
To   <[email protected]>
Subject   st: Re: first program
Date   Thu, 1 Jun 2006 09:55:53 -0400

Dirk,

If you are planning to run this program only in one dataset (that uses id as 
cross-sectional identifier) you dont need to put it in the syntax. When you 
put id in the syntax you are creating a temporary variable called id that 
will be use further, for that reason you have to invoke it using `id' 
instead of id.

You are not using [if] and [in], putting these in the syntax line just 
allows to use them. This means that the users can conditional the result but 
your program still needs to apply the restrictions... read marksample to get 
a formal uses of [if] and [in].

You have to define a temporary variable count in order to prevent that this 
variable already exists in the dataset. Again, you have to invoke it using 
`count' instead of count. After keep you will lose information, be careful 
with that.

Finally, I don't understand what did you mean with "compile". You just load 
the program with -do balance- or even more, you can save this program with 
the extension .ado into your personal folder and this will be your own 
command.


Rodrigo.
PS: This is my version of your program:

program balance
    version 8.2
    syntax [if] [in]
    tempvar count
    qui     {
        local ivar: char _dta[iis]
        by `ivar': egen `count'=count(`ivar') `if' `in'
        sum `count'
        local max=r(max)
        keep if `count'==`max'
    }
end

I dropped rclass, because I don't need to save any value after reducing the 
panel. Also, I deleted id of the syntax because you can use char _dta[iis] 
that tells you which cross-sectional variable was defined using -tsset-. 
Note that local variables as well [if] and [in] are invoked using `'.


----- Original Message ----- 
All
I am trying to write my first Stata program and was wondering if someone 
could go through it and tell me if it's correct, how I should refer to id 
and what rclass means (just copied that).
Another thing, I compiled it once and then wanted to recompile it. How do I 
do that?
Thanks, Dirk
/*
program to balance an unbalanced panel, keep only those individuals with the 
max duration
*/
program balance, rclass
version 8.2
syntax id [if] [in]
qui     {
    sort id
    by id: egen count=count(id)
    sum count
    local max=r(max)
    keep if count==max
    }
end

Dirk Nachbar
Assistant Economist
Pensim2
Department for Work and Pensions
Level 4, The Adelphi
1-11 John Adam St
WC2N 6HT London
020 796 28531 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index