The: setvar will only work in SQL command mode, Hence you are possibly within the normal SQL execution in the management studio and have not swapped to command mode.
It can also be done through the user interface in SQL Server Management Studio by using the "Query" menu, and selecting "SQLCMD mode."