Dear StataList.

I need to merge two sets of data (partner + respondent) per wave for many waves. The complication is that while the control variables appear in each wave (though some begin in wave 2, not wave 1), some variables (of interest) appear irregularly and inconsistently (every 3 or 4 years). How can I deal with this merge and apply a loop for all waves? I read over https://www.statalist.org/forums/new-content/51 and @Cox and Kantor suggested there may be better options than the "if, then" command (which I attempted using without success below).
Note: this is a very large dataset (000s of variables) ...
Code:
*partner data
local p = substr("abcdefghijklmnopqrstuv",`wave',1)
if wave == d g j n {
use waveid age sex empstat educ inc marstat nlpreg workhr relb relimp relat using "c:/data/Combined_a170c.dta", clear
}
else {
 if wave == a b c e f h i k l m o p q     // all waves a-q, excl d, g, j, n
use waveid age sex empstat educ inc marstat nlpreg workhr using "c:/data/Combined_a170c.dta", clear
}
rename `p'* p_* // replace wave with partner data prefix
  rename waveid hhid
sort hhpxid
 save "c:/data/temp", replace

*respondent data
local p = substr("abcdefghijklmnopqrstuv",`wave',1)
if wave == d g j n {
use waveid hhid age sex empstat educ inc marstat nlpreg workhr relb relimp relat using "c:/data/Combined_a170c.dta", clear
}
else {
 if wave == a b c e f h i k l m o p q     // all waves a-q, excl d, g, j, n
use waveid hhid age sex empstat educ inc marstat nlpreg workhr using `readdatadir'/Combined_q170c.dta, clear
}
rename `p'* * drop if hhid==""
 sort hhid

merge 1:1 hhid using "c:/data/temp", replace
// I cannot figure out how to then loop this for all other waves?