Variables in AMPscript

AMPscript variable names always begin with the at sign (@).

Declaring Variables

You can declare a variable using the Var keyword. When you declare a keyword, it is added to a dictionary of variables. The value of a newly declared variable is null. You can only declare variables in AMPscript blocks. In other words, you can't declare variables in inline AMPscript strings.

This example declares a variable called "firstName".

%%[
  Var @firstName
]%%

You can use the Var keyword to declare multiple values in a single line. This example declares the "firstName", "lastName", and "contactCity" variables.

%%[
  Var @firstName, @lastName, @contactCity
]%%

Setting Variables

Use the Set keyword to assign a new value to a variable. This example declares the variable "contactCity" and sets its value to "Tokyo".

%%[
  Var @contactCity
  Set @contactCity = "Tokyo"
]%%

You can set the value of a variable to the value of a function. This example sets the "currentDate" variable to the current date using the FormatDate() and Now() functions.

%%[
  Var @currentDate
  Set @currentDate = FormatDate(Now(), "d MMMM yyyy")
]%%

If the current date is August 5, 2023, the "currentDate" variable would have the value "5 August 2023".