Mishkat al-Masabih 82
Ibn Mas'ud said that God’s messenger who spoke the truth and whose word was believed told them the following:
The constituents of one of you are collected for forty days in his mother’s womb in the form of a drop, then they become a piece of congealed blood for a similar period, then they become a lump of flesh for a similar period. Then God sends to him an angel with four words who records his deeds, the period of his life, his provision, and whether he will be miserable or blessed: thereafter He breathes the spirit into him. By Him other than whom there is no god, one of you will do the deeds of those who go to paradise so that there will be only a cubit between him and it, then what is decreed will overcome him so that he will do the deeds of those who go to hell and will enter it; and one of you will do the deeds of those who go to hell so that there will be only a cubit between him and it, then what is decreed will overcome him so that he will do the deeds of those who go to paradise and will enter it. 

(Bukhari and Muslim.)
عَن عبد الله بن مَسْعُود قَالَ: حَدَّثَنَا رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ وَهُوَ الصَّادِق المصدوق: «إِن أحدكُم يجمع خلقه فِي بطن أمه أَرْبَعِينَ يَوْمًا ثمَّ يكون فِي ذَلِك علقَة مثل ذَلِك ثمَّ يكون فِي ذَلِك مُضْغَة مثل ذَلِك ثمَّ يُرْسل الْملك فينفخ فِيهِ الرّوح وَيُؤمر بِأَرْبَع كَلِمَات بكتب رزقه وأجله وَعَمله وشقي أَو سعيد فوالذي لَا إِلَه غَيره إِن أحدكُم لَيَعْمَلُ بِعَمَلِ أَهْلِ الْجَنَّةِ حَتَّى مَا يَكُونُ بَيْنَهُ وَبَيْنَهَا إِلَّا ذِرَاعٌ فَيَسْبِقُ عَلَيْهِ الْكِتَابُ فَيَعْمَلُ بِعَمَلِ أَهْلِ النَّارِ فَيَدْخُلُهَا وَإِنَّ أَحَدَكُمْ لَيَعْمَلُ بِعَمَلِ أَهْلِ النَّارِ حَتَّى مَا يَكُونُ بَيْنَهُ وَبَيْنَهَا إِلَّا ذِرَاعٌ فَيَسْبِقُ عَلَيْهِ الْكِتَابُ فَيَعْمَلُ بِعَمَلِ أَهْلِ الْجَنَّةِ فَيَدْخُلُهَا»