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

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

From   Steinar Fossedal <>
Subject   st: Programming: Capturing bysort-parameters for use within a program
Date   Tue, 24 Mar 2009 22:53:48 +0100

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'

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.

*   For searches and help try:

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