In this example I expect FINDW to trim the BE value in element and find a match. It does not.
The only case FINDW works for me is when there is an explicit trim of the word and the T modifier is left out.
SAS Help Center: FINDW Function
data _null_;
length element $8;
element = 'BE';
found_at = findw('H He Li Be B C N O F Ne',element,' ','it');
put found_at= ;
found_at = findw('H He Li Be B C N O F Ne',element,' ',1,'it');
put found_at= ;
delims = ' ' ;
found_at = findw('H He Li Be B C N O F Ne',element,delims,'it');
put found_at= ;
delims = ' ' ;
found_at = findw('H He Li Be B C N O F Ne',element,1,delims,'it');
put found_at= ;
found_at = findw('H He Li Be B C N O F Ne',trim(element),' ','it');
put found_at= 'explicit trim I and T modifiers' ;
found_at = findw('H He Li Be B C N O F Ne',trim(element),' ','i');
put found_at= 'explicit trim only I modifier';
found_at = findw('Be H He Li B C N O F Ne',trim(element),' ','it');
put found_at= 'explicit trim I and T modifiers, Be first position' ;
run;
found_at=0
found_at=0
found_at=0
found_at=0
found_at=0 explicit trim I and T modifiers
found_at=9 explicit trim only I modifier
found_at=0 explicit trim I and T modifiers, Be first position
The T modifier removes the spaces. Add the space back into the delimiter list by using the S modifier.
s or S
adds space characters (blank, horizontal tab, vertical tab, carriage return, line feed, and form feed) to the list of characters.
t or T
trims trailing blanks from the string, word, and character arguments.
Try this:
data _null_;
length element $8;
element = 'BE';
found_at = findw('H He Li Be B C N O F Ne',element,' ','sit');
put found_at= ;
found_at = findw('H He Li Be B C N O F Ne',element,' ',1,'sit');
put found_at= ;
delims = ' ' ;
found_at = findw('H He Li Be B C N O F Ne',element,delims,'sit');
put found_at= ;
delims = ' ' ;
found_at = findw('H He Li Be B C N O F Ne',element,1,delims,'sit');
put found_at= ;
found_at = findw('H He Li Be B C N O F Ne',trim(element),' ','sit');
put found_at= 'explicit trim I and T modifiers' ;
found_at = findw('H He Li Be B C N O F Ne',trim(element),' ','si');
put found_at= 'explicit trim only I modifier';
found_at = findw('Be H He Li B C N O F Ne',trim(element),' ','sit');
put found_at= 'explicit trim I and T modifiers, Be first position' ;
run;
Result
found_at=9 found_at=9 found_at=9 found_at=9 found_at=9 explicit trim I and T modifiers found_at=9 explicit trim only I modifier found_at=1 explicit trim I and T modifiers, Be first position
The T modifier removes the spaces. Add the space back into the delimiter list by using the S modifier.
s or S
adds space characters (blank, horizontal tab, vertical tab, carriage return, line feed, and form feed) to the list of characters.
t or T
trims trailing blanks from the string, word, and character arguments.
Try this:
data _null_;
length element $8;
element = 'BE';
found_at = findw('H He Li Be B C N O F Ne',element,' ','sit');
put found_at= ;
found_at = findw('H He Li Be B C N O F Ne',element,' ',1,'sit');
put found_at= ;
delims = ' ' ;
found_at = findw('H He Li Be B C N O F Ne',element,delims,'sit');
put found_at= ;
delims = ' ' ;
found_at = findw('H He Li Be B C N O F Ne',element,1,delims,'sit');
put found_at= ;
found_at = findw('H He Li Be B C N O F Ne',trim(element),' ','sit');
put found_at= 'explicit trim I and T modifiers' ;
found_at = findw('H He Li Be B C N O F Ne',trim(element),' ','si');
put found_at= 'explicit trim only I modifier';
found_at = findw('Be H He Li B C N O F Ne',trim(element),' ','sit');
put found_at= 'explicit trim I and T modifiers, Be first position' ;
run;
Result
found_at=9 found_at=9 found_at=9 found_at=9 found_at=9 explicit trim I and T modifiers found_at=9 explicit trim only I modifier found_at=1 explicit trim I and T modifiers, Be first position
It comes to light that that T modifier is not to be used when the only delimiter character is a space. T will trim the space to nothing, and the function will happily fart in your general direction with no delimiters.
R is the proper modifier for the case of a space delimiter.
I was usually happy to use STRIP() instead of T modification.
7 data _null_;
8 length element $8;
9 element = 'BE';
10 found_at = findw('H He Li Be B C N O F Ne',strip(element),' ','i');
11 put found_at= ;
12 run;
found_at=9
Dive into keynotes, announcements and breakthroughs on demand.
Explore Now →Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.