*! version 0.0.1 jsl 2008-01-29

capture program drop pvsel3
program define pvsel3
    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]

    * globals pvshowif and pvhideif
    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 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" {
            global pv`hideshow'if "$pv`hideshow'if & ( `anyif' )"
        }
        else if "`or'"=="or" {
            global pv`hideshow'if "$pv`hideshow'if | ( `anyif' )"
        }
        else {
            global pv`hideshow'if "(`anyif')"
        }
    } // any option

    * selection based on all in 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

    if "`test'"=="test" {
        di "Ending status:" _new
        di "show if: $pvshowif"
        di "hide if: $pvhideif"
    }

end

exit

HISTORY