/* IML-IF-THEN-stuff-09dec10.sas ON EXAMPLES link on course web page */ options formdlim = "+"; proc iml; start mytest(x); if x<0 then; print "x<0 condition is true and executing IF X<0 step ..."; print "x negative"; if x=0 then; print "x=0 condition is true and executing IF X=0 step ..."; print "x = 0"; else do; print "executing the ELSE DO step ..."; mysqrt = sqrt(x); print "sqrt of X = " x " is " mysqrt; end; finish mytest; /* Produces the following errors ... ERROR: ELSE does not follow IF statement at line=67 col=3. ERROR: Module MYTEST was not defined due to resolution errors. */ quit; proc iml; start mytest2(x); print "STARTING mytest2 WITH x = " x; if x<0 then do; print "x<0 condition is true and executing IF X<0 step ..."; print "x negative"; end; if x=0 then do; print "x=0 condition is true and executing IF X=0 step ..."; print "x = 0"; end; else do; print "executing the ELSE DO step ..."; mysqrt = sqrt(x); print "sqrt of X = " x " is " mysqrt; end; finish mytest2; run mytest2(0); run mytest2(16.25); run mytest2(-2); quit; /* Produces the following error ... ERROR: (execution) Invalid argument to function. operation : SQRT at line 136 column 19 operands : x x 1 row 1 col (numeric) -2 statement : ASSIGN at line 136 column 6 traceback : module MYTEST2 at line 136 column 6 */ proc iml; start mytest3(x); print "STARTING mytest3 WITH x = " x; if x<0 then do; print "x<0 condition is true and executing IF X<0 step ..."; print "x negative"; end; ELSE if x=0 then do; print "x=0 condition is true and executing ELSE IF X=0 step ..."; print "x = 0"; end; else do; print "executing the ELSE DO step ..."; mysqrt = sqrt(x); print "sqrt of X = " x " is " mysqrt; end; finish mytest3; run mytest3(0); run mytest3(16.25); run mytest3(-2); quit; /* This module does what you want! */ proc iml; start newmod(x); print ' '; print 'Entering NEWMOD with x= ' x; if x<0 then; print 'Error'; else; if x=0 then; print 'Zero'; else; do; if x>1 then; do; print 'Bigger >1'; end; if x<=1 then; do; print 'Less than or EQ 1'; end; end; finish newmod; run newmod(-1); run newmod(0); run newmod(0.5); run newmod(1.5); quit; /* works but not clear ... next module is arguably preferred ... */ proc iml; start newmod2(x); print ' '; print 'Entering NEWMOD2 with x= ' x; if x<0 then print 'Error'; else if x=0 then print 'Zero'; else do; if x>1 then print 'Bigger >1'; else print 'Less than or EQ 1'; end; finish newmod2; run newmod2(-1); run newmod2(0); run newmod2(0.5); run newmod2(1.5); quit;