Aiden Markram and Rassie van der Dussen led the fightback as the Proteas reached stumps on day four on 127-1, still requiring 243 more runs for victory.
Monday is set for a gripping conclusion to the Test series between the Proteas and Pakistan with both teams eying victory.
It will, however, be a big test of the South Africans’ character, who have suffered numerous batting collapses on the tour.
Mohammad Rizwan struck an unbeaten century as Pakistan added valuable runs during the morning and middle session on Sunday.
The Proteas bowlers were made to work hard to wrap up the tail as Pakistan made it to 298 before being bowled out, setting the visitors a target of 370 to chase.
George Linde managed to pick up the final wicket of Shaheen Afridi, which handed him his first ever five-wicket haul in Test cricket, finishing with figures of 5-64 in 26 overs.
Keshav Maharaj ended with three wickets of his own, while Kagiso Rabada had two scalps in the 14 overs he bowled.
In response Aiden Markram and Dean Elgar made a decent start with the bat, although Markram took 20 deliveries to get off the mark.
Elgar, however, departed furiously for 17 when he nicked one behind off a Afridi delivery. Rassie van der Dussen joined Markram in the middle and in contrasting style to his first-innings golden duck, looked calm and in control.
The pair batted well and took the scoring opportunities on offer, while defending for the lives in the process.
The two batsmen completed the important task of making it to stumps without losing any of their wickets.
Markram reached an important half-century and will resume Monday’s action on 59 with Van der Dussen on 48.