BookmarkSubscribeRSS Feed
sasprogramming
Quartz | Level 8

I am using the following code to find the word DI. The code below returns true when it looks at "CREDIT". However I want it to return false, DI is in CREDIT, but I only want cases true when DI is a word. How do i achieve this?

 

if findw(field_to_search_from, 'DI') ge 1 then flag= 1;
6 REPLIES 6
jimbarbour
Meteorite | Level 14

I would use the FIND or FINDW functions to identify distinct words.

 

For example: 

DATA	_NULL_;
					*----+----1----+----2----+----3----+----4----+----5-;
	String		=	'In Army boot camp, the DI is in charge.';

	Position	=	FINDW(String, 'DI');

	PUTLOG  "The position is " Position 2.;
RUN;

 Yields:

The position is 24

 

Jim

andreas_lds
Jade | Level 19

Please post the code you have including data in usable form (data step with datalines, no attachments).

Using findw is the right choice.

Josie1
Obsidian | Level 7

try putting a space before and after like ' DI '

sasprogramming
Quartz | Level 8

But if CREDIT is the first word in the field_to_search_from then it wont have a space before it?

Josie1
Obsidian | Level 7

not unless you write CRE DI T

andreas_lds
Jade | Level 19

@Josie1 wrote:

try putting a space before and after like ' DI '


Not necessary if findw is used.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 506 views
  • 1 like
  • 4 in conversation