Wei Yan followed Liu Bei into Shu at the head of his retainer troops, won merit in battle many times, and was promoted to General of the Standard (yamen jiangjun). Records, Shu 10 §57
When Liu Bei became King of Hanzhong and was to move his seat to Chengdu, an important general was needed to guard the Han River region. Everyone assumed the post would go to Zhang Fei, and Zhang Fei privately thought so too, but Liu Bei chose Wei Yan, making him Area Commander of Hanzhong (du Hanzhong) and General Who Guards the Distant (zhenyuan jiangjun) and concurrently Administrator of Hanzhong, to the astonishment of the whole army. When Liu Bei asked him before the assembled officials what he intended, Wei Yan replied that if Cao Cao came with the forces of the whole realm, he would hold them off for the Great King, and if a subordinate general came with a host of 100,000, he would swallow it up for the Great King. Liu Bei approved, and everyone admired the boldness of his words. Records, Shu 10 §57–58Records, Shu 2 §54
The biography of Jiang Wei records that when Liu Bei first left Wei Yan to guard Hanzhong, every outer defensive enclosure was fully manned so that invaders could not get in, and that Wang Ping's later success in holding off Cao Shuang in the battle of Xingshi also followed this system. Records, Shu 14 §41
When Liu Bei assumed the imperial title, Wei Yan became General Who Guards the North, and in the 1st year of Jianxing (223) he was enfeoffed as Marquis of a Chief Precinct (duting hou). In the 5th year of Jianxing (227), when Zhuge Liang was stationed in Hanzhong, he made Wei Yan Commander of the Front Division (du qianbu), concurrently Major to the Chancellor and Inspector of Liang Province. The next year, when Zhuge Liang marched out to Mount Qi, people said veteran generals such as Wei Yan and Wu Yi should lead the vanguard, but Zhuge Liang went against the general view and chose Ma Su. Records, Shu 10 §58Records, Shu 9 §17
In the 8th year of Jianxing (230), Zhuge Liang sent Wei Yan west into the lands of the Qiang. When Wei's General of the Rear Fei Yao and Guo Huai, Inspector of Yong Province, met him in battle at Yangxi, Wei Yan inflicted a heavy defeat on Guo Huai and the others, and he was promoted to Military Adviser of the Front (qian junshi) and General-in-Chief Who Conquers the West, granted the Tally of Authority (jiajie), and raised in rank to Marquis of Nanzheng. Records, Shu 10 §58Records, Shu 3 §11
Each time he went on campaign with Zhuge Liang, Wei Yan asked for 10,000 men so that he could take a separate route from Zhuge Liang and meet him at Tong Pass, following the old precedent of Han Xin, but Zhuge Liang would not allow it. Wei Yan always said that Zhuge Liang was timid, and resented that his own talents were not fully used. He cared well for his soldiers and surpassed others in valor, but he was arrogant by nature and everyone avoided him; only Yang Yi would not give way, and the two were like water and fire. The biography of Fei Yi records that whenever the two sat together they quarreled, and Wei Yan would sometimes raise his sword at Yang Yi, until Fei Yi stepped between them and separated them. Records, Shu 10 §59Records, Shu 14 §24
In the 12th year of Jianxing (234), when Zhuge Liang marched out through the mouth of the Northern Valley, Wei Yan led the vanguard. Ten li out from the camp, he dreamed that horns grew on his head and asked Zhao Zhi about it; Zhao Zhi glossed it over, saying that the qilin has horns but does not use them, a sign that the enemy would break apart of its own accord without a battle. Then, after withdrawing, Zhao Zhi told others that the character for "horn" (角) is "knife" (刀) with "use" (用) below it, so it meant using a knife on one's head—a most ill omen. Records, Shu 10 §59–60
In the autumn, as Zhuge Liang's illness became critical, he settled with Yang Yi, Fei Yi, Jiang Wei, and others the arrangements for withdrawing the army after his death: Wei Yan was to cover the rear, with Jiang Wei next, and if Wei Yan would not comply, the army was simply to leave. When Zhuge Liang died, Yang Yi sent Fei Yi to sound out Wei Yan's intentions. Wei Yan said that though the Chancellor was dead, he himself was still alive and would lead the armies to attack the enemy, and asked how he could be put under Yang Yi's command as a rearguard general. Fei Yi made the excuse that he would go and talk Yang Yi round, and galloped away. Records, Shu 10 §61–62
When the various camps began returning one after another as Zhuge Liang had arranged, Wei Yan was furious; he went south ahead of them, burning the plank roads wherever he passed. Wei Yan and Yang Yi each memorialized that the other was a traitor, and when the Later Sovereign, Liu Shan, asked about it, Dong Yun and Jiang Wan vouched for Yang Yi and cast suspicion on Wei Yan. Wei Yan seized the mouth of the Southern Valley and attacked Yang Yi, but when He Ping (Wang Ping) rebuked Wei Yan's vanguard, saying that the Chancellor's body was not yet cold, the soldiers knew that the fault lay with Wei Yan and scattered. Records, Shu 10 §62–64Records, Shu 13 §38
Wei Yan fled to Hanzhong with a few of his sons. Yang Yi sent Ma Dai to pursue and behead him, and when Ma Dai presented the head, Yang Yi trampled on it, saying, "Wretch! Can you do evil again?" Wei Yan's three sets of kin were exterminated. Chen Shou wrote that Wei Yan's turning south rather than going north to surrender to Wei was only because he wanted to get rid of Yang Yi and the others, and that he had hoped public opinion would make him Zhuge Liang's successor, not that he meant to rebel. Records, Shu 10 §64–65
In his assessment, Chen Shou said that Wei Yan was employed for his valor and strategy and held in high esteem, but that the disaster he invited and the blame he incurred all arose from himself. Yang Xi's Eulogies on the Supporting Officials of Ji Han said that he was discordant and, forgetting his integrity, spoke of rebellion, and that to loathe his end while regretting his beginning was truly owing to his nature. Records, Shu 10 §74Records, Shu 15 §62