This was tested under Natural for Windows. You'll have to see how it fares on the mainframe.
DEFINE DATA LOCAL
1 #A (A5) INIT <'ab'>
1 #B (A5)
1 #C (C)
1 #L (L) INIT <TRUE>
END-DEFINE
REPEAT
INPUT (AD=MDL'_')
'A:' #A (CV=#C)
// 'B:' #B
IF #L
THEN
RESET #L
IF #A = ' '
THEN
ASSIGN #C = (AD=PN)
ELSE
ASSIGN #C = (AD=P)
COMPRESS #A
H'0000000000'
INTO #A LEAVING NO SPACE
END-IF
ELSE
ASSIGN #L = TRUE
ASSIGN #C = (AD=D)
EXAMINE #A FOR H'00' DELETE
END-IF
END-REPEAT
END
If the field is empty, the CV is set to non-display (AD=N). Otherwise the filler is replace with non-displayable characters (H'00'). For display (ie to revert to underscores) the hex zeros are deleted.
This works for alpha fields; I haven't come up with a solution for numerics, yet.