The Number 1 independent website for ACL information!

TexasACL User Group
About Us
Training
Why ACL
Essays & Scripts
ACL News
Events
Links
FAQ
Site Map
Support Us
Variable Substitution Part 2
Obtaining user input is only one of the occasions where you might want to use a variable.  There are numerous other occassions where variable substitution will come in handy.
 
Conditions from the data itself
 
Often times while performing various analysis, you might want to use variables to make the project flexible to the actual conditions of the data.  For example, suppose that you want to identify the ten percent of the population that had the most activity over a given month.  Suppose you have a table that is sorted highest total dollar amount to lowest dollar amount. You might use something like:
 
OPEN TABLE
COUNT <---this will count the number of records and provide a variable COUNT1 with the number of records.
EXTRACT RECORD IF RECNO() <= COUNT1 *0.10
 
Now, let's suppose that you've identified the person with the most activity in the given month and now you want to identify the specific transactions for the user.  If you were to manually do this, you might identify the user_name and use a filter:

User_Name = "Porter Broyles"
 
Well, since you are putting this into a script that you want to work on different months, hard coding an individuals name won't work.  Instead, you might write something like this (assuming the list is sorted so that the person with the highest activity is first.)
 
OPEN summary_table
LOCATE RECORD 1
v_user_name = User_name
 
OPEN detail_table
SET FILTER User_name = "%v_user_name%"
 
GROUPS

When using the percentage signs to surround the variable, it is called MACRO substitution.  MACRO substitution cannot be used within GROUPS.  GROUPS are an advanced script writing technique that allows the programmer to script advanced analytics on a project. A GROUP will perform the actions within the GROUP on the record before moving to the next record, whereupon it will repeat the actions.  A simple GROUP might have you comparing a value from one record to another record. For example suppose that you want to EXTRACT records to a new table where the USER name is the same as the previous record.  The following GROUP might be used:

v_user_name = BLANK(25)

GROUP
  EXTRACT FIELDS ALL TO TEMP1 IF v_user_name = username
  v_user_name = username
END

In the above example, we've defined a variable v_user_name.  In the GROUP, ACL will look at the first record and compare that to the current value of v_user_name which is defined as 25 blanks.  If the statement is true, then it will extract the record to a file TEMP1.  ACL will then define the value of the variable v_user_name as the value of the field username for record 1.  It will then repeat the process for record two and so on.

Because variable substitution is not an option in GROUPs, GROUPS have some limitations.  For example, while you can use numeric, logical, or variables that represent fields, you cannot use ASCII characters in GROUPS and expect the value to be changed.
LISTS
 
Another common use for variables is when they appear in lists.  A list might contain the names of the top posters to the ACL User Forum:

v_list = "CAATS; Porter; GJH; JGP"
 
The first thing to notice is that the list itself can be preserved as a variable!  The second thing is that you can then use variables to target specific users.  For example, suppose that you have written a script or LOOP that is designed to look at each of the forum contributors individually, you might write a code that isolates each using the SPLIT command:
v_poster = SPLIT("%v_list%",v_cnt)
v_cnt= v_cnt +1