The office of Viceroy of Shaan-Gan originated in the early Ming dynasty with the garrisoning of military forces in three towns along the northern border of Shaanxi Province.
In 1497, when the Mongols of the Northern Yuan dynasty made intrusions across the border, the Hongzhi Emperor put Wang Yue (王越) in charge of coordinating military actions in Shaanxi, Yansui, Ningxia and Gansu.
Yang Yiqing (楊一清) led lightly armed cavalry to launch a surprise attack on the Mongols and succeeded in driving them back.
After this incident, Yang Yiqing realized that military operations in Yansui, Ningxia and Gansu had to be better coordinated, hence he urged the imperial court to appoint a viceroy to oversee the three areas.
In 1723, the Yongzheng Emperor ordered that all Viceroys who also held the position of Secretary of War (兵部尚書) would concurrently be appointed as Right Censor-in-Chief (右都御史) of the Detection Branch (都察院) in the Censorate.