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
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.
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
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;
@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;
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.
data have;
input a $ b $;
flag=ifn(find(b,a,'it'),1,0);
datalines;
123abc one
123abc two
123abc 123abc45
;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.