Without any more info than that it’s hard to say. I’d start making sure the 2wd/4wd switch has voltage. And sends voltage out when put in 4wd. If so I’d follow it. Not sure if it makes any stops otw bc I’m not near my SM but I’d check at transfer case. And of course fuses come first.
That’s what I wanted to say lol. Glad u did bc it sounded better than how I was saying it haha. When he said it went in and out I thought of solenoid sticking, or a broken wire that was still hit or miss making the circuit and it finally broke or came all the way loose
once you get underneath the rig you will see the solenoid on the rear of the transfer case left of the output shaft, there are 2 bolts holding it on, be careful the bolts may be hard to unscrew, a forum member said one of there bolts broke in half ( steel bolt in aluminium casing).
could still be a harness wiring or connector issue, or switch.