You probably want a calculate script on your second field, something like;
xfa.resolveNode("SSN[0]").isNull
? ""
: '***-**-'+xfa.resolveNode("SSN[0]").rawValue.substr(-4)
You will need to change the "xfa.resolveNode("SSN[0]")" bits to reference your fields.