* ================================================================== * File: do_repeat_example03.SPS . * Date: 5-Aug-2011 . * Author: Bruce Weaver, bweaver@lakeheadu.ca . * Notes: Replace a long DO-IF-ELSE-IF with a simple DO-REPEAT . * ================================================================== . * This file shows an example of how to replace * a lenghty nested DO-IF structure with a much * shorter (and easier to read) DO-REPEAT structure. * The DO-IF was posted to the SPSSX-L mailing list * in August 2011, and can be seen here: * http://spssx-discussion.1045642.n5.nabble.com/Unclosed-loop-tp4669655p4669655.html . * Create some sample data. new file. dataset close all. data list free / reldate (date11). begin data 15-Jan-1999 31-Mar-1999 1-Apr-1999 15-Jan-2000 31-Mar-2000 1-Apr-2000 15-Jan-2001 31-Mar-2001 1-Apr-2001 15-Jan-2002 31-Mar-2002 1-Apr-2002 15-Jan-2003 31-Mar-2003 1-Apr-2003 15-Jan-2004 31-Mar-2004 1-Apr-2004 15-Jan-2005 31-Mar-2005 1-Apr-2005 15-Jan-2006 31-Mar-2006 1-Apr-2006 15-Jan-2007 31-Mar-2007 1-Apr-2007 end data. * Here is the original nested DO-IF. Do if reldate >= date.mdy(4,1,1999) and reldate <= date.mdy(3,31,2000). - comp fy=1. ELSE if reldate >= date.mdy(4,1,2000) and reldate <= date.mdy(3,31,2001). - comp fy=2. ELSE if reldate >= date.mdy(4,1,2001) and reldate <= date.mdy(3,31,2002). - comp fy=3. ELSE if reldate >= date.mdy(4,1,2002) and reldate <= date.mdy(3,31,2003). - comp fy=4. ELSE if reldate >= date.mdy(4,1,2003) and reldate <= date.mdy(3,31,2004). - comp fy=5. ELSE if reldate >= date.mdy(4,1,2004) and reldate <= date.mdy(3,31,2005). - comp fy=6. ELSE if reldate >= date.mdy(4,1,2005) and reldate <= date.mdy(3,31,2006). - comp fy=7. ELSE if reldate >= date.mdy(4,1,2006) and reldate <= date.mdy(3,31,2007). - comp fy=8. END IF. * And here is the DO-REPEAT that accomplishes the same thing. * It uses far fewer keystrokes, and is easier to follow IMO. do repeat i = 1 to 8 / y1 = 1999 to 2006 / y2 = 2000 to 2007. - if RANGE(reldate,date.mdy(4,1,y1),date.mdy(3,31,y2)) fy2 = i. end repeat. execute. list. * Notice that FY and FY2 have the same values. * ================================================================== .