How-To: Create an age range without using RECODE or IF

Files needed: none

Level of experience: Beginner

Version(s) Supported: Epi Info™ 3.5.1

Before going to deep into this guide, let's discuss the concept of an 'Age Range.' What is an age range, and why is it important? An age range is nothing more than a series of ages, but grouped into categories. For example, we might say everyone between the ages of 0 and 10 are in the "Under 10" age category. Everyone who is 11 to 20 might fall into the "11 - 20" category. Creating a range of values (meaning, several categories of age ranges) is important for analysis. Think about creating a graph of ages; if we showed all ages, we might have 70 or 80 lines on our graph. In addition to looking cluttered, it might be very difficult to read. By consolidating those ages into smaller, and often times more logical groupings (infants, children, teens, young adults, etc), we can create better output.

This guide will cover the process by which users can create an age range or other range of numbers in the Analysis module without having to use the RECODE or IF commands. Bypassing RECODE and IF is extremely important, since not only do those commands take a while to write, but they have a size limitation; generally, more than 12 conditions in either one will "break" the command and cause Epi Info to throw an error (there are workarounds to bypass this limit but they also involve some work and data manipulation). Using the method described below, an age range can be created with just a few short lines of code and can support as many recoded numbers as the user wants.

The trick to making this work is simple mathematics, which we implement using Epi Info's math operators and the TRUNC() function.

See the code below:

READ 'C:\Epi_Info\Sample.mdb':viewOswego

ASSIGN AgeRange = TRUNC(age / 10) * 10


The above code will create age ranges, in increments of 10, from 0 to as high as the 'Age' variable goes, and then output them to the AgeRange temporary variable. This code can be copied/pasted directly into your Analysis module's Program Editor and run, as it uses the Sample.mdb file which is included with every copy of Epi Info. The LIST command at the end of the script will display the AgeRange variable, which is the output, and the Age variable, which is the data that was used to create AgeRange.

As you can see, a numeric range of numbers was created without needing to use the traditional IF or RECODE commands. The drawback is that instead of having a "Under 10" and "10 - 20" label, they are simply numbers. We can work around this problem by using the FORMAT() function and the concatenation operator (&). See below:

READ 'C:\Epi_Info\Sample.mdb':viewOswego


ASSIGN AgeRangeTemp = TRUNC(age / 10) * 10
ASSIGN AgeRange = Format(AgeRangeTemp, "0") & " to " & Format(AgeRangeTemp + 9, "0")

LIST AgeRange AgeRangeTemp Age GRIDTABLE

In this case, we have actually created an AgeRange variable that is a string, with meaningful labels (e.g. "10 - 19"). Please copy/paste into your Analysis program editor and click RUN to see the results.

What if we want to see AgeRange in increments of 15, rather than 10? The answer is to change the numeric constants from 10 to 15:

READ 'C:\Epi_Info\Sample.mdb':viewOswego

ASSIGN AgeRange = TRUNC(age / 15) * 15


Note that the TRUNC() command cannot create different increments in the same range all by itself. For example, if I wanted AgeRange to increment by 5 for those aged zero to 20 years, and then increment by 10 for all years afterwards, I would need to use an IF statement. See:

READ 'C:\Epi_Info\Sample.mdb':viewOswego


IF Age < 20 THEN
    ASSIGN AgeRange = TRUNC(AGE / 5) * 5
    ASSIGN AgeRange = TRUNC(AGE / 10) * 10


While the last piece of program editor code (shown above in gray) does require use one IF command to generate a variable incrementation, the IF command contains only one condition.  If we created the range using IF commands without TRUNC, many more IF commands would be required.

Note that this method will not work for text variables unless those text variables are numbers. For example, "Five" in a drop-down field would not work; "5", however, would work. In the latter case, the TXTTONUM() function can convert the "5" (text) into a number (5), and the same methods as above would be available to the user.

Recoding actual words still needs to be done with IF and RECODE, although depending on the words themselves, the STRLEN() and SUBSTRING() function can act similarly to TRUNC() and might be able to work some of the same tricks we pointed out above. More information on all of these commands, functions, and operators can be found in the Epi Info help file, which is accessible by going to Help > Contents from the main Epi Info menu.


  • It is possible to recode a number variable to a range of values without using the IF or RECODE commands
  • Bypassing the RECODE and IF commands does not work with text variables, unless those text variables contain solely numbers
  • RECODE and IF commands have size limitations and don't generally allow more than 12 variations, which is why this trick can be useful for recoding large number ranges
  • You can create meaningful labels, such as "10 - 20", by using the FORMAT() function and the & operator.
  • Using TRUNC() instead of RECODE or IF still allows for flexibility when different types of incrementing in the same range is desired.

Disclaimer: Use of the files and methods described in this document should be done at your own risk. The opinions expressed are solely those of the author and do not reflect the official position of any organization or agency in any way on any topic. All names, systems, services and data are fictitious. Any similarity to real people or places is coincidental.

Tags: Age Range, Analysis, How-To, HowTo, RECODE, Range

Views: 524

Replies to This Discussion

thanks brother it's amazing work
it is excellent and very important for me as a person works in the statistics
by using the normal interfaces without analyze code i mean in ASSIGN window, so please can you just explain it
for me again by using interface?



© 2015   Created by phConnect Admin.   Powered by

 |  Report an Issue  |  Terms of Service