Scope

Once a variable is set, its scope is set globally; that is, it can be referenced later in the code to display the output, or used in conditional expressions. However, variables can also be changed after they have been set. For example, a promotion end date is set from a field in a Sendable Data Extension using a personalization string, but for specific customers, there is a requirement to adjust this value later in the code:

%%[

var @promotionEndDate, @memberStatus
set @promotionEndDate = '10/15/2018'
set @memberStatus = status

if @memberStatus == 'gold' then
   set @promotionEndDate = '11/15/2018'
endif

]%%

Variables can also be set on themselves in a function; that is, if the variable has previously been declared, then it can be used as an argument to a function, based on its previous value. So the above code could also be expressed as:

%%[

var @promotionEndDate, @memberStatus
set @promotionEndDate = '10/15/2018'
set @memberStatus = status

if @memberStatus == 'gold' then
   set @promotionEndDate = DateAdd(@promotionEndDate, 1, 'M')
endif

]%%

Also, once a variable has been set, it can be reset by declaring the value again. In the example below, if the current date is less than (after) a membership end date, then a null value will be set for membership status.

%%[

var @membershipEndDate, @memberStatus
set @membershipEndDate = '10/15/2018'
set @memberStatus = status

if @membershipEndDate > Now() then
   var @memberStatus
endif

]%%

The only exception to this scope rule is when a variable is used within a for block. In this scenario, the variable is locked from modification within the process loop.