CSPro 7.4 has introduced a new and improved recode statement that will allow you to get more done in a single recode. To see the differences between the old and new recode you will compare implementations of a typical education edit. Note that the old recode has been deprecated.
Specification
The goal of the two recode implementations will be to verify that students currently attending school are within the allowable age range for their grade. The specfication below defines the valid combination of grades and ages.
Old Recode Implementation
Using the old recode syntax notice that you will need two recode statements. One for the minimum age and another for the maximum age.
preproc
ask if P09_ATTEND = 1 and P04_AGE >= 3; // Currently attending school
postproc
numeric minAgeForGrade, maxAgeForGrade;
recode P10_GRADE_NOW_ATTENDING => minAgeForGrade;
0 => 3; // Preschool and kindergarten
1 => 5;
2 => 6;
3 => 7;
4 => 8;
5 => 9;
6 => 10;
7 => 11;
8 => 12;
9 => 13;
10 => 14;
11 => 15;
12 => 16;
13 => 16; // University but not graduate school
14 => 18; // Graduate school
15 => 15; // Trade or technical school
endrecode;
recode P10_GRADE_NOW_ATTENDING => maxAgeForGrade;
0 => 6; // Preschool and kindergarten
1 => 8;
2 => 9;
3 => 10;
4 => 11;
5 => 12;
6 => 13;
7 => 14;
8 => 15;
9 => 18;
10 => 20;
11 => 21;
12 => 22;
13 => 95; // University but not graduate school
14 => 95; // Graduate school
15 => 95; // Trade or technical school
endrecode;
if P04_AGE in minAgeForGrade:maxAgeForGrade then
errmsg("P10_GRADE_NOW_ATTENDING(=%d) is valid for age(=%d)", $, P04_AGE);
else
errmsg("P10_GRADE_NOW_ATTENDING(=%d) NOT valid for age(=%d)", $, P04_AGE);
reenter;
endif;
Syntax Change Between Old and New Recode
To make use of the new recode statement you will have to write your recodes with a slighly modified syntax. The table below documents these changes.
Operator | Old Recode Syntax | New Recode Syntax |
---|---|---|
Assignment | => | -> |
And | : | :: |
Range | - | : |
New Recode Implementation
With this new recode statement you can determine the minimum and maximum ages within a single recode. Making your logic easier to understand and maintain.
preproc
ask if P09_ATTEND = 1 and P04_AGE >= 3; // Currently attending school
postproc
numeric minAgeForGrade, maxAgeForGrade;
recode P10_GRADE_NOW_ATTENDING -> minAgeForGrade :: maxAgeForGrade;
0 -> 3 :: 6; // Preschool and kindergarten
1 -> 5 :: 8;
2 -> 6 :: 9;
3 -> 7 :: 10;
4 -> 8 :: 11;
5 -> 9 :: 12;
6 -> 10 :: 13;
7 -> 11 :: 14;
8 -> 12 :: 15;
9 -> 13 :: 18;
10 -> 14 :: 20;
11 -> 15 :: 21;
12 -> 16 :: 22;
13 -> 16 :: 95; // University but not graduate school
14 -> 18 :: 95; // Graduate school
15 -> 15 :: 95; // Trade or technical school
endrecode;
if P04_AGE in minAgeForGrade:maxAgeForGrade then
errmsg("P10_GRADE_NOW_ATTENDING(=%d) is valid for age(=%d)", $, P04_AGE);
else
errmsg("P10_GRADE_NOW_ATTENDING(=%d) NOT valid for age(=%d)", $, P04_AGE);
reenter;
endif;
Flag Implementation
Another approach is to create a test flag. In the logic below, grade_is_valid is used to show whether or not the combination of grades and ages are valid. This can further increase the readability of your logic.
preproc
ask if P09_ATTEND = 1 and P04_AGE >= 3; // Currently attending school
postproc
numeric grade_is_valid = false;
recode P10_GRADE_NOW_ATTENDING :: P04_AGE -> grade_is_valid;
0 :: 3:6 -> true; // Preschool and kindergarten
1 :: 5:8 -> true;
2 :: 6:9 -> true;
3 :: 7:10 -> true;
4 :: 8 11 -> true;
5 :: 9:12 -> true;
6 :: 10:13 -> true;
7 :: 11:14 -> true;
8 :: 12:15 -> true;
9 :: 13:18 -> true;
10 :: 14:20 -> true;
11 :: 15:21 -> true;
12 :: 16:22 -> true;
13 :: 16:95 -> true; // University but not graduate school
14 :: 18:95 -> true; // Graduate school
15 :: 15:95 -> true; // Trade or technical school
endrecode;
if grade_is_valid then
errmsg("P10_GRADE_NOW_ATTENDING(=%d) is valid for age(=%d)", $, P04_AGE);
else
errmsg("P10_GRADE_NOW_ATTENDING(=%d) NOT valid for age(=%d)", $, P04_AGE);
reenter;
endif;