BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
publicSynechism
Fluorite | Level 6

I want to take a dataset like this:

data have;
	input a $ b $;
	datalines;
123abc one
123abc two
123abc 123abc45
;
run;

And flag when variable 'a' is fully contained within variable 'b',

such that I return this table:

data want;
	input a $ b $ flag;
	datalines;
123abc one 0
123abc two 0
123abc 123abc45 1
;
run;

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
tsap
Pyrite | Level 9

I believe this will accomplish the results you are looking for:

DATA WORK.WANT;
	SET WORK.HAVE;
		 IF FIND(TRIM(b),TRIM(a),'i') GE 1 THEN DO; FLAG=1;	END;
	ELSE IF FIND(TRIM(b),TRIM(a),'i') LT 1 THEN DO; FLAG=0; END;
RUN;

Hope this helps.

View solution in original post

6 REPLIES 6
Reeza
Super User

Have you tried the FIND() or INDEX() functions?

 


@publicSynechism wrote:

I want to take a dataset like this:

data have;
	input a $ b $;
	datalines;
123abc one
123abc two
123abc 123abc45
;
run;

And flag when variable 'a' is fully contained within variable 'b',

such that I return this table:

data want;
	input a $ b $ flag;
	datalines;
123abc one 0
123abc two 0
123abc 123abc45 1
;
run;

Thanks


 

publicSynechism
Fluorite | Level 6

Yes, but I believe it's returning 0 whether it finds the string or not, since it will return the start position of the found string. It will always be the case in my data that startpos = 0.

data have2;
	set have;
	out=find(a,b,'t');
	out2=index(trim(a),b);
run;
Reeza
Super User

@publicSynechism wrote:

Yes, but I believe it's returning 0 whether it finds the string or not, since it will return the start position of the found string. It will always be the case in my data that startpos = 0.

data have2;
	set have;
	out=find(a,b,'t');
	out2=index(trim(a),b);
run;

Which is why the check is for Greater than or equal to 1. 

 

From the documentation:

The FIND function searches string for the first occurrence of the specified substring, and returns the position of that substring. If the substring is not found in string, FIND returns a value of 0.

 

Setting the flag as follows will always return a 0 or 1. 

 

FLAG = find(b, a, 'it') >= 1;

 

tsap
Pyrite | Level 9

I believe this will accomplish the results you are looking for:

DATA WORK.WANT;
	SET WORK.HAVE;
		 IF FIND(TRIM(b),TRIM(a),'i') GE 1 THEN DO; FLAG=1;	END;
	ELSE IF FIND(TRIM(b),TRIM(a),'i') LT 1 THEN DO; FLAG=0; END;
RUN;

Hope this helps.

Reeza
Super User
It can be as simple as:

FLAG = find(b, a, 'it') GE 1;
Ksharp
Super User
data have;
	input a $ b $;
	flag=ifn(find(b,a,'it'),1,0);
	datalines;
123abc one
123abc two
123abc 123abc45
;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1991 views
  • 4 likes
  • 4 in conversation