*! version 0.5.0 jsl 2010-02-12
*  2010-10-24 pvdata to pvdata4

//  specify the selection conditions that are used to control which paths
//  are shown in the graph.

capture program drop pvsel4
program define pvsel4
    version 9.2
    local qui "qui"

    syntax ,            ///
        [Reset]         /// reset selection
        [any(string)]   /// any of these categories
        [all(string)]   /// any of these categories
        [and]           /// and with existing condition
        [hide]          /// hide if or show if
        [test]          /// test mode

//  set up condition to show or hide?
    local hideshow "show"
    if "`hide'"=="hide" {
        local hideshow "show"
    }

//  get names of variables in dataset
    local sortvars : char _dta[pvsortvars]
    local itemvars : char _dta[pvitemvars]
    local othervars : char _dta[pvothervars]

//  define globals pvshowif and pvhideif that control what to show or hide
    if "`test'"=="test" {
        di "Starting status:" _new
        di "show if: $pvshowif"
        di "hide if: $pvhideif"
    }

//  reset existing show/hide conditions
    if "`reset'"=="reset" {
        global pvshowif ""
        global pvhideif ""
    }

//  selection based on any items in category
    if "`any'"!="" {
        * loop through categories
        local anyif ""
        foreach cat in `any' {
            foreach itm in `itemvars' {
                * | makes it any path that is in this category
                local anyif "`anyif' | `itm'==`cat'"
            }
        }
        * get rid of extra bar
        local anyif = subinstr("`anyif'","|","",1)
        * append to existing conditions
        if "`and'"=="and" { // append as and condition
            global pv`hideshow'if "$pv`hideshow'if & ( `anyif' )"
        }
        else if "`or'"=="or" { // append as or condition
            global pv`hideshow'if "$pv`hideshow'if | ( `anyif' )"
        }
        else {
            global pv`hideshow'if "(`anyif')"
        }
    } // any option

//  selection if always found in given category
    if "`all'"!="" {
        * loop through categories
        local allif ""
        local cat : word 1 of `all'
        foreach itm in `itemvars' {
            local allif "`allif' & `itm'==`cat'"
        }
        * get rid of extra bar
        local allif = subinstr("`allif'","&","",1)
        * append to existing conditions
        if "`and'"=="and" {
            global pv`hideshow'if "$pv`hideshow'if & ( `allif' )"
        }
        else if "`or'"=="or" {
            global pv`hideshow'if "$pv`hideshow'if | ( `allif' )"
        }
        else {
            global pv`hideshow'if "(`allif')"
        }
    } // all option

//  show the test condition that was created
    if "`test'"=="test" {
        di "Ending status:" _new
        di "show if: $pvshowif"
        di "hide if: $pvhideif"
    }

end
exit

HISTORY

* version 0.0.1 jsl 2008-01-29
* version 0.0.2 jsl 2009-10-11
*   - check and clean code