Skip to main Content

Useful String Functions: strip, scanq, substrn

strip(string)  - Removes leading and trailing blanks in one go. This is the same as left(trim(string)) that you would use in V8.

scanQ(string,position) – The scanq function allows the returning of separated words within a string like the scan function e.g. scan(‘Andy Bowden’,-1) = ‘Bowden’. The scanq function however allows you to ignore separators enclosed in quotation marks.

data _null_;
 return=scanq("Andy'-'Bowden'-'Amadeus-Software Ltd",2,'-');
 put return=;
run;


Running this produces the following in the log:

5 data _null_;
6   return=scanq("Andy'-'Bowden'-'Amadeus-Software Ltd",2,'-');
7   put return=;
8 run;
return=Software Ltd
NOTE: DATA statement used (Total process time):
      real time           0.10 seconds
      cpu time            0.00 seconds


substrn() – One of the annoying things with the substr function is the note you get in the log when the function returns a blank value.

data _null_;
 return=substr("Andy Bowden",20,2);
run;


will produce the following in the log:

9  data _null_;
10   return=substr("Andy Bowden",20,2);
11 run;
NOTE: Invalid second argument to function SUBSTR at line 10 
column 12.
return=  _ERROR_=1 _N_=1
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


A way around this is to use the substrn function, which allows the returning of missing values.

data _null_;
 return_2nd=substrn("Andy Bowden",20,2);
run;


Using substrn will produce the following in the log:

data _null_;
 return_2nd=substrn("Andy Bowden",20,2);
run;