I have a piece of AMPscript containing 99 problems server variables which I would like to loop through and print, one by one. They're named [AX_VAR_1] through [AX_VAR_99]. I don't want to list out all 99 of them individually, but I cannot find a way to refer to them dynamically. Is this possible in AMPscript?

Thank you!

EDIT: Clarification and Update

I don't have any control over the data, the client does. We have a lot of (sequential) server variables that drive data for several different types of sends; some activate various content areas, others insert personalization data, still others determine the creative or the imagery to be used. Not all of the variables are used for every send.

What I want to do, though, is display them all as a table at the bottom of proof emails for QA purposes (i.e. to make sure that the logic is working correctly as it changes every send). I don't want to insert blank ones if I can help it.

Since this is for internal QA, it doesn't have to be fast (the maximum send will only be around 100 or so records). It also doesn't have to be particularly bulletproof, just accurate. And it doesn't have to be pretty, because we will be sending emails without this data to the client for proofing.

Thank you again!

  • This is typically a data problem. Why can't you create a row set and loop through that? Apr 3, 2014 at 22:32
  • Would you be willing to post the use case that would require this? Maybe we might have some alternatives.
    – Timothy
    Apr 4, 2014 at 15:43
  • I've updated the question to address these issues. Thanks! Apr 4, 2014 at 15:55

3 Answers 3


I'm not sure why you would have 99 variables, but if you really needed to do this you would have to use SSJS and AMPScript together.

The following code requires that the variable names are the same except for an incrementing integer value, and also that you know the number of variables that exist ahead of time.

    set @AX_VAR_1 = "Variable 1"
    set @AX_VAR_2 = "Variable 2"
    set @AX_VAR_3 = "Variable 3"
    set @AX_VAR_4 = "Variable 3"
    set @numVars = 4

<script runat="server">
    Platform.Load("Core", "1")

    var numVars = Variable.GetValue("@numVars");
    for (x = 0; x <= numVars; x++)
        Write(Variable.GetValue("@AX_VAR_" + x));
        Write("<br />");
  • Not sure what this would do to your send speeds with the ssjs. Most likely there is a better approach here. Apr 4, 2014 at 1:49
  • It's just for internal QA, so the send speeds aren't really an issue (maximum send size will only really ever be about 100 records). I have 99 variables, but individual subscribers really only ever have about 20-30 of them in use at a time. It's just not always the same 20-30. All the variables I'm talking about are incrementing and I do know how many exist in advance, so this might work out. If there's not an AMPscript way to do this, I'll run with this. Thanks. Apr 4, 2014 at 15:56
  • 1
    Make sure to check the answer if this works for you. Apr 4, 2014 at 16:06
  • I wanted to start on the implementation before I selected the answer. I know how frustrating it can be when you get and then lose points on a StackExchange site. :-) But so far, this is looking like a great way of doing it. I hadn't considered using SSJS and AMPscript together, but when I started writing it up, it made perfect sense. Thanks again! Apr 5, 2014 at 4:25

You can do something like this


SET @names = "name1;name2;name3;name4"
SET @nameSet = BuildRowSetFromString(@names, ";") //creating a dummy rowset of variables I would like to dynamically create.

FOR @i = 1 TO ROWCOUNT(@nameSet) DO

SET @row = Row(@nameSet, @i)
SET @name = Field(@row, 1)

TreatAsContent( Concat( "%", "%[ SET @", @name, " = 'a value' ]%", "%") )



What this code is doing is creating Ampscript Variables within AMPScript dynamically. It's creating a variable from my rowset, so - @name1, @name2, @name3 but with a value of "a value"

  • Nice tip. I had reverted to using SSJS until I saw this. I couldn't work out the order of operations, but this helped. Thanks! Jul 11, 2022 at 22:07

OK, I have a better understanding of what you're trying to accomplish. Since you are looking for test results, this is how I would do it.

  1. Create a DE from [AX_VAR_1] through [AX_VAR_99]. These need to be nullable. Put some additional columns on that DE that will serve as the composite primary key, like emailaddress + jobid + emailid (see http://help.exacttarget.com/en/documentation/exacttarget/content/personalization_strings/ for those).
  2. Create a Content Area that pushes the fields of interest. Since [AX_VAR_1] through [AX_VAR_99] are nullable, you may or may not push them all. Obviously, you need the keys.
  3. Insert that content area on the emails you want to test. The push happens at send time.
  4. Create another DE consisting of 4 columns, the 3 column composite key and "results" (or whatever you like.).
  5. Write a query activity to SELECT the primary key(s) from the 1st DE, and Coalesce/concatenate the useful fields into the results column.
  6. Create a landing page that consists of that table you WERE going to put in that email. Pass jobid, emailid, and emailid in the query string
  7. Put a link to that landing page in the email, passing in the columns that compose the primary key as parameters.
  8. Put the send and query activity in an automation. The query won't run until the send is complete, but since this is just to test, that's fine.
  9. When you get the test email, click the landing page link, and see if you get the results you want.
  • 1
    Interesting approach, but its a bit convoluted. All the OP wants to do is print the values in the DE he is sending to, most likely so he can QC the emails. This approach requires landing pages, programs and queries to accomplish something that can be done with a little AMPscript and SSJS. Just my two cents.
    – Jon Sakas
    Apr 4, 2014 at 21:23
  • I appreciate the workthrough on this, but I agree with jsakas - that's really complicated for what will just end up being a few lines that only I and 2-3 other ET employees will ever end up seeing. I've started implementing the other solution and it looks like it'll be just what I need. Apr 5, 2014 at 4:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .