Problem Note 40815: Nested IF statements with multiple ELSE statements might require DO/END statements in IML
Nested IF statements with multiple ELSE statements might generate an error message even if the statements are syntactically correct. This error occurs only in "immediate mode," which means that PROC IML is executing statements in the main scope of the program. The problem can also occur in SAS/IML Studio if you are submitting statements in "statement mode."
For example, the following PROC IML statements generate an error:
proc iml;
n=1; x=2; y=1;
if n>0 then
if x>y then w = x;
else w = y;
else w = 0;
print w;
quit;
Resulting SAS log
1 proc iml;
NOTE: IML Ready
2 n=1;
2 ! x=2;
2 ! y=1;
3 if n>0 then
4 if x>y then w = x;
5 else w = y;
6 else w = 0;
ERROR: ELSE does not follow IF statement at line=6 col=4.
7 print w;
8 quit;
NOTE: Exiting IML.
This is a common problem known with many programming languages that is commonly referred to as a “Dangling Else/If-Else Ambiguity”.
When the SAS/IML parser encounters the first ELSE statement, the parser terminates the original IF statement regardless of nesting depth. Consequently, the second else no longer matches any pending IF statements and an error occurs.
DO/END statements can be used to direct the SAS/IML parser on how to interpret nested IF statements, as shown in the following example.
proc iml;
n=1; x=2; y=1;
if n>0 then do;
if x>y then w = x;
else w = y;
end;
else w = 0;
print w;
quit;
Another option for a workaround is to define a module like below:
proc iml;
start test;
n=1; x=2; y=1;
if n>0 then
if x>y then w = x;
else w = y;
else w = 0;
print w;
finish;
run test;
quit;
Operating System and Release Information
SAS System | SAS/IML | z/OS | | |
OpenVMS VAX | | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
Microsoft Windows XP 64-bit Edition | | |
Microsoft® Windows® for x64 | | |
OS/2 | | |
Microsoft Windows 95/98 | | |
Microsoft Windows 2000 Advanced Server | | |
Microsoft Windows 2000 Datacenter Server | | |
Microsoft Windows 2000 Server | | |
Microsoft Windows 2000 Professional | | |
Microsoft Windows NT Workstation | | |
Microsoft Windows Server 2003 Datacenter Edition | | |
Microsoft Windows Server 2003 Enterprise Edition | | |
Microsoft Windows Server 2003 Standard Edition | | |
Microsoft Windows Server 2003 for x64 | | |
Microsoft Windows Server 2008 | | |
Microsoft Windows Server 2008 for x64 | | |
Microsoft Windows XP Professional | | |
Windows 7 Enterprise 32 bit | | |
Windows 7 Enterprise x64 | | |
Windows 7 Home Premium 32 bit | | |
Windows 7 Home Premium x64 | | |
Windows 7 Professional 32 bit | | |
Windows 7 Professional x64 | | |
Windows 7 Ultimate 32 bit | | |
Windows 7 Ultimate x64 | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
Windows Vista for x64 | | |
64-bit Enabled AIX | | |
64-bit Enabled HP-UX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
AIX | | |
HP-UX | | |
HP-UX IPF | | |
IRIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |
*
For software releases that are not yet generally available, the Fixed
Release is the software release in which the problem is planned to be
fixed.
Nested IF statements with multiple ELSE statements might generate an error message even if the statements are syntactically correct.
Type: | Problem Note |
Priority: | medium |
Topic: | Analytics ==> Matrix Programming SAS Reference ==> Procedures ==> IML
|
Date Modified: | 2010-09-16 15:26:49 |
Date Created: | 2010-09-08 07:15:53 |