Create an age range without using RECODE or IFFiles needed:
noneLevel of experience:
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:
ASSIGN AgeRange = TRUNC(age / 10) * 10
LIST AgeRange Age GRIDTABLE
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:
DEFINE AgeRangeTemp NUMERIC
DEFINE AgeRange TEXTINPUT
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:
ASSIGN AgeRange = TRUNC(age / 15) * 15
LIST AgeRange Age GRIDTABLE
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:
IF Age < 20 THEN
ASSIGN AgeRange = TRUNC(AGE / 5) * 5
ASSIGN AgeRange = TRUNC(AGE / 10) * 10
LIST Age AgeRange GRIDTABLE
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
from the main Epi Info menu.
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.
- 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.