Dear STATA users,

I am using the code below and have encountered two problems which I could not solve.
The first of these is: How can I define the local upports and downports as a variable selection of variables based on the characters included in the variable names? For instance I tried:
Code:
local upports ds VWES*A6
Additionally how do I make sure this variable list only includes variables which contain observations (as the program will not run if there are no observations). My best guess:
Code:
foreach varname of varlist _all {
    quietly sum `varname'
    if `r(N)'==0 {
        if `varname' has VWES*A6 {
        local upports ds`varname'
        }
    }
}
set more off
The reason being that I need to run multiple of these with variations in the variables, so doing this process by hand is not feasible.

My second problem is, I need to run a conditional test (besides the two tests already in the code). Specifically, while I first test:
Code:
test (_b[_cons]=0)
I then need to test whether beta equals 1:
Code:
_b[`:word `i' of `upports'']=1
CONDITIONAL ON (alpha equaling zero):
Code:
_b[_cons]=0
.
This is not the same as the joint test:
Code:
test (_b[_cons]=0) (_b[`:word `i' of `upports'']=1)
Here is my complete code:
Code:
clear
cls
use "I:\dta files\FINAL.dta", clear

local upports VWESTOA6  VWIAMIA6  VWSOWEA6  EWWAEAA6  EWENSOA6 ///
VWESAFA6  VWIANOA6  VWGOTOA6  EWWAMIA6  EWENWEA6 ///
VWESASA6  VWIAPAA6  VWGOAFA6  EWWANOA6  EWSOTOA6 ///
VWESEAA6  VWIASOA6  VWGOASA6  EWWAPAA6  EWSOAFA6  ///
VWESMIA6  VWIAWEA6  VWGOEAA6  EWWASOA6  EWSOASA6 ///
VWESNOA6  VWENTOA6  VWGOMIA6  EWWAWEA6  EWSOEAA6 ///
VWESPAA6  VWENAFA6  VWGONOA6  EWIATOA6  EWSOMIA6 ///
VWESSOA6  VWENASA6  VWGOPAA6  EWIAAFA6  EWSONOA6 ///
VWESWEA6  VWENEAA6  VWGOSOA6  EWIAASA6  EWSOPAA6 ///
VWWATOA6  VWENMIA6  VWGOWEA6  EWIAEAA6  EWSOSOA6 ///
VWWAAFA6  VWENNOA6  EWESTOA6  EWIAMIA6  EWSOWEA6 ///
VWWAASA6  VWENPAA6  EWESAFA6  EWIANOA6  EWGOTOA6 ///
VWWAEAA6  VWENSOA6  EWESASA6  EWIAPAA6  EWGOAFA6 ///
VWWAMIA6  VWENWEA6  EWESEAA6  EWIASOA6  EWGOASA6 ///
VWWANOA6  VWSOTOA6  EWESMIA6  EWIAWEA6  EWGOEAA6 ///
VWWAPAA6  VWSOAFA6  EWESNOA6  EWENTOA6  EWGOMIA6 ///
VWWASOA6  VWSOASA6  EWESPAA6  EWENAFA6  EWGONOA6 ///
VWWAWEA6  VWSOEAA6  EWESSOA6  EWENASA6  EWGOPAA6 ///
VWIATOA6  VWSOMIA6  EWESWEA6  EWENEAA6  EWGOSOA6 ///
VWIAAFA6  VWSONOA6  EWWATOA6  EWENMIA6  EWGOWEA6 ///
VWIAASA6  VWSOPAA6  EWWAAFA6  EWENNOA6 ///
VWIAEAA6  VWSOSOA6  EWWAASA6  EWENPAA6

local downports VWESTOL6  VWIAMIL6  VWSOWEL6  EWWAEAL6  EWENSOL6 ///
VWESAFL6  VWIANOL6  VWGOTOL6  EWWAMIL6  EWENWEL6 ///
VWESASL6  VWIAPAL6  VWGOAFL6  EWWANOL6  EWSOTOL6 ///
VWESEAL6  VWIASOL6  VWGOASL6  EWWAPAL6  EWSOAFL6 ///
VWESMIL6  VWIAWEL6  VWGOEAL6  EWWASOL6  EWSOASL6 ///
VWESNOL6  VWENTOL6  VWGOMIL6  EWWAWEL6  EWSOEAL6 ///
VWESPAL6  VWENAFL6  VWGONOL6  EWIATOL6  EWSOMIL6 ///
VWESSOL6  VWENASL6  VWGOPAL6  EWIAAFL6  EWSONOL6 ///
VWESWEL6  VWENEAL6  VWGOSOL6  EWIAASL6  EWSOPAL6 ///
VWWATOL6  VWENMIL6  VWGOWEL6  EWIAEAL6  EWSOSOL6 ///
VWWAAFL6  VWENNOL6  EWESTOL6  EWIAMIL6  EWSOWEL6 ///
VWWAASL6  VWENPAL6  EWESAFL6  EWIANOL6  EWGOTOL6 ///
VWWAEAL6  VWENSOL6  EWESASL6  EWIAPAL6  EWGOAFL6 ///
VWWAMIL6  VWENWEL6  EWESEAL6  EWIASOL6  EWGOASL6 ///
VWWANOL6  VWSOTOL6  EWESMIL6  EWIAWEL6  EWGOEAL6 ///
VWWAPAL6  VWSOAFL6  EWESNOL6  EWENTOL6  EWGOMIL6 ///
VWWASOL6  VWSOASL6  EWESPAL6  EWENAFL6  EWGONOL6 ///
VWWAWEL6  VWSOEAL6  EWESSOL6  EWENASL6  EWGOPAL6 ///
VWIATOL6  VWSOMIL6  EWESWEL6  EWENEAL6  EWGOSOL6 ///
VWIAAFL6  VWSONOL6  EWWATOL6  EWENMIL6  EWGOWEL6 ///
VWIAASL6  VWSOPAL6  EWWAAFL6  EWENNOL6 ///
VWIAEAL6  VWSOSOL6  EWWAASL6  EWENPAL6 ///


local n_upports: word count `upports'

capture postutil clear
tempfile results
postfile handle str20(downportname upportsname) float(alph beta) /// 
float(Fintersection pintersection) float(Fspanning pspanning) ///
using `results'
 
forvalues i = 1/`n_upports' {
    reg `:word `i' of `downports'' `:word `i' of `upports''
    test (_b[_cons]=0)
    local F_intersection = r(F)
    local p_intersection = r(p)
    test (_b[_cons]=0) (_b[`:word `i' of `upports'']=1)
    local F_spanning = r(F)
    local p_spanning = r(p)
    post handle ("`:word `i' of `downports''") ("`:word `i' of `upports''") (_b[_cons]) ///
    (_b[`:word `i' of `upports'']) (`F_intersection') (`p_intersection') ///
    (`F_spanning') (`p_spanning')
}
set more off
postclose handle
use `results', clear
export excel using "I:\Spanning tests\Spanningtests - Above 60.xls", sheetreplace firstrow(variables)
Thank you very much in advance!