Instead of looping you'd probably have to utilize If-Else functionality.
Use several expressions, and assign priorities to each quality code in the correct order.
Name your first expression "FirstTag" and do: If(Tag1="") Then 1 Else If(Tag1="A") Then 2 etc.
Do the same for "SecondTag".
In the third expression you can compare the priorities of "FirstTag" and "SecondTag" to determine which should be used, and then output the correct value.
Another way to is create an AF Table with the quality codes and priorities. Then, create a Table Lookup DR for each tag quality Attribute and compare the Table Lookup attribute values in the AF analyses.
Thank you Asle and Barry for your responses. I found a simple way to accomplish this by using built-in "InStr" string function. I used this function to assign priorities to each DQ tag:
If 'tag1|qualityCode'="" then 100 else InStr("<>?SIEWA", String('tag1|qualityCode'))
I compared the DQ priorities and the lowest value of the two was chosen as final DQ.