Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Programming: Capturing bysort-parameters for use within a program


From   Eva Poen <[email protected]>
To   [email protected]
Subject   Re: st: Programming: Capturing bysort-parameters for use within a program
Date   Tue, 24 Mar 2009 22:46:54 +0000

Steinar,

is your program about missing values? Or was it just an example?
Martin already pointed you towards an FAQ which may help you if the
missing values are your problem.

If it was only an example, and you still need access to the sortvars,
here is a suggestion.
If I understand correctly, a byable(onecall) program, if -by-ed,
guarantees that the data are sorted according to the -by- statement.
You could therefore create a tempvar and use that for sorting. Like:

program whatever, byable(onecall)
  syntax varlist [if]
  ...
  tempvar sort
  gen `sort' = _n
  bysort `touse' (`sort') : ...
end

Also note that after calling -qui describe, varlist-, the macro
r(sortlist) contains the sort order of the data.

Hope this helps,
Eva



2009/3/24 Steinar Fossedal <[email protected]>:
> Hi, listers
>
> I'm having trouble capturing the parameters for bysort used when
> calling a program. The following simple (and untested) program
> illustrates the problem. It replaces missing values with the
> previously known value, using only a subset of the data.
>
> capture program drop missing_like_last
> program missing_like_last, byable(onecall)
>      syntax varlist [if]
>      marksample touse
>      bysort `touse' `_byvars' (`varlist2-reference'): replace `var' =
> `var'[_n-1] if `var'==. & `touse'
> end
>
> bysort id (sortvar): missing_like_last myvar if somevar==1
>
> (syntax for bysort is: bysort varlist1 (varlist2): ...)
>
> As you can see, I'm trying to issue commands using the exact same
> bysort-parameters as used when calling the program, just adding the
> `touse' variable. I need the order to be correct as I am issuing
> commands with references between rows ( [_n-1] ).
>
> To do this, I need to capture the -bysort- parameters varlist1 and
> varlist2 containing the values id and sortvar. -_byvars- takes care of
> varlist1, but how can I capture varlist2? Without specifying varlist2,
> the sort order within id will be scrambled.
>
> I hope you can help.
>
> Cheers,
> -Steinar

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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