Welcome to TRS
Not a great swap, but doable
4.0l SOHC 207HP
4.0l OHV 160HP
so losing 47 horse power
They use the same motor mounts and trans bell housing pattern
You will need to swap in the 1999 wiring harness and computer
Both need to be automatics or both manuals or the computers won't swap
And this is where there are differences
1999 gets Speed from GEM module
2002 gets Speed from computer, and computer is gone
You will need to use an SGI-100BT speed interface to get speedo/odo working and for shifting if its an automatic
The power and ground wires are different so you will need to match those up for the engine harness
2002 has PATS(passive anti-theft system) in the computer
1999 has a separate PATS module above glove box
You will need to swap the PATS module over and its harness
And swap out the ignition cylinders to uses the 1999 PATS keys in the 2002
Need to swap over exhaust system as well
All in all I would take the FREE 1999 and sell it, then use that money to rebuild the 2002, lol
Its pretty much the same work to swap in a 5.0l V8 but you end up with a 2002 V8 Ranger, which in my opinion is better than a 2002 Ranger with 25% less power