>>I'll check into that in the diagnoses table and source tables.
Wise. To immediate review there is no obvious explanation for additional segments inside the string, except field values from the diagnoses table. Example: it may well be that in the dim distant past, somebody created or edited placeholder row/s in diagnoses for certain special cases, putting ^^| in diagnosis_code and diagnosis_code_qualifier to mimic the empty/not found state. I've seen that before for some of the "not otherwise qualified" codes that the user wants to store but to be treated as blank for billing or other purposes. That's fine except that it causes nested ^^| in your code. If so, be careful changing it, in case you transfer the breakage somewhere else! Might be better to alter your IF along the lines of
IF THIS.m_seek(ALLTRIM(STR(rx.rx_no)) + ALLTRIM(STR(refill_no)),"diagnoses","fullrx",1) and diagnoses.diagnosis_code !="^^|"
Or if you're sure it's used nowhere else, you can replace diagnosis_code and diagnosis_code_qualifier with "" for diagnosis_code="^^|" or even delete the row if it's not part of an official codeset lookup.
If it's not from diagnoses then I agree it's a twilight zone issue!
"... They ne'er cared for us
yet: suffer us to famish, and their store-houses
crammed with grain; make edicts for usury, to
support usurers; repeal daily any wholesome act
established against the rich, and provide more
piercing statutes daily, to chain up and restrain
the poor. If the wars eat us not up, they will; and
there's all the love they bear us."
-- Shakespeare: Coriolanus, Act 1, scene 1