Something like the following would allow you to create arrays as you read thru each of your files. The following STARTS the coding for awk to read and set some variables to arrays. This still needs logic on what to write out to a work (output) file, and also ENDING logic. [Don't want to do everything for you.]
If you think this might be a way to go, you could start with this coding and research on how to do the remaining awk commands necessary.
awk -v wrk1=$workfile1ns -v wrk2x=$workfile2x '
BEGIN {
FSf1=","
FSf2=","
}
FNR == 1 {
if (FILENAME==wrk2x) FS=FSf2
if (FILENAME==wrk1) FS=FSf1
$0=$0
}
FILENAME==wrk1 {
medx[$1]=$2
}
FILENAME==wrk2x{
ua1_pl[$1]=$2
}