Yoga Sutra - Sādhana Pada 2.7
सुखानुशयी रागः ॥७॥
sukha-anuśayī rāgaḥ ||7||
Attraction is clinging to pleasure.
Attachment stems from [experiences] of happiness.
Moving on to the third kleśa, Vyāsa simply says that the hankering, desire, or craving for pleasure, sukha, or the means to attain pleasure by one who remembers past experiences of pleasure is attachment, rāga. The key ingredient in this process is memory. One who has experienced pleasure in the past recollects it and hankers to repeat the experience in the present or future, or to attain the means of repeating the experience; it is this dwelling on past experiences that constitutes attachment. Vācaspati Miśra adds that ego is the root of attachment, just as ignorance is the root of ego; consequently, ego precedes attachment in the list of kleśas as ignorance precedes ego.
The commentators outline the psychology of attachment in the following manner: When a new means of pleasure is perceived, it is memory that infers that this new means of pleasure is the same as or similar to something that produced pleasure in the past, and hence it promises to provide the same or similar pleasure in the present or future. Therefore, memory precedes attachment, that is, attachment is predicated on memory. Hariharānanda adds that previous impressions, saṁskāras, of pleasure can remain latent in the mind, and thus even when memory is not consciously activated, these latent saṁskāras cause the mind and senses to be unconsciously drawn toward objects that have produced pleasure in the past. Hence one might find oneself partial to something for no particular conscious reason, which, from the perspective of Yoga psychology, could correspond to latent imprints from previous lives (the phenomenon of déjà vu is explainable in similar manner). Ignorance and ego cause the deluded mind to associate the self with these latent saṁskāras as well, identifying the self with the senses through which these latent impulses toward pleasure can be expressed. These kleśas thus cause the mind to identify the self with the nonself, namely, the body and the mind.
Hariharānanda also makes the important observation that when desire deepens into greed, the sense of right and wrong, morality, becomes neglected. The stronger the greed, the more a person is liable to pursue immoral means of obtaining the objects of desire. The Gītā outlines the sequence of events:
From contemplating the objects of the senses, an attachment to them is born, from attachment, desire arises, and from desire is produced anger. From anger comes illusion, and from illusion, intelligence is destroyed, and from the loss of intelligence, one is lost. (II.62–63)
Vijñānabhikṣu adds as an aside that the desire of the jīvanmukta, or liberated but still embodied soul (most probably a reference to the desire to help other embodied beings), is not an attachment at all and thus not a kleśa perpetuating saṁsāra. This is because the desire of a liberated soul is not for personal pleasure or gratification, or, ultimately, stemming from ignorance at all.