Pages

Tuesday, 13 September 2011

NOTE: Substringing Without Notes

The SUBSTR function is a much-used function but can be frustrating when it issues an abrupt NOTE to the log when the string isn't long enough to produce a result.

If the design of your code means that you're happy to allow a null result from your sub-string function, consider using SUBSTRN. This function provides the same essential functionality as SUBSTR but will not issue a NOTE message if it returns a null result. The two DATA steps below show the contrast in results:

15 data _null_;
16   MyString = 'Shorty';
17   a = substr(MyString,1,10);
18   b = substr(MyString,15,10);
19   put a= b= ;
20 run;

NOTE: Invalid third argument to function SUBSTR at line 17 column 8.
NOTE: Invalid second argument to function SUBSTR at line 18 column 7.
a=Shorty b=
MyString=Shorty a=Shorty b= _ERROR_=1 _N_=1
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

21
22 data _null_;
23   MyString = 'Shorty';
24   an = substrN(MyString,1,10);
25   bn = substrN(MyString,15,10);
26   put an= bn=;
27 run;

an=Shorty bn=


Notice in particular how the attempt to assign a value to B where the starting position is beyond the length of the string produces _ERROR_ set to 1.

In the right situation, SUBSTRN can be very handy.