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

 

Hi everyone

 

I would like to add a variable to my data that indicates wether or not the value in V1 matches the value in V2. 

V1 is actually mostly empty, most rows don't contain a value. If there is a value, it can be any combination of the values possible for V2 separated by a comma (,). I've sketched a bit of an example. 

 

More general, I want to check if the string/character(?) in V2 shows up in V1, regardless of order or size. 

 

V1V2Match
 a10
a1, b1a11
b2, b3, c6b21
c4, b2b21
BOa10

 

I've tried something with find, but I think I get an error related to the missings in V1?

 

DATA my_data;

 Match = FIND(Var1, Var2) gt 0;

RUN;

 

I'm very new to SAS, so sorry if I made a silly mistake. Thanks a bunch though!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Get rid of leading/trailing blanks in the string you are searching for:

data have;
infile datalines dlm="09"x dsd truncover;
input V1 $ V2 $;
datalines;
 	a1
a1, b1	a1
b2, b3, c6	b2
c4, b2	b2
BO	a1
;

data want;
set have;
match = findw(v1,strip(v2)," ,") > 0;
run;

And please supply your example data in the future in manner like above (data step with datalines), so we immediately have something to develop and test code against without having to make guesses about variable attributes and content.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Get rid of leading/trailing blanks in the string you are searching for:

data have;
infile datalines dlm="09"x dsd truncover;
input V1 $ V2 $;
datalines;
 	a1
a1, b1	a1
b2, b3, c6	b2
c4, b2	b2
BO	a1
;

data want;
set have;
match = findw(v1,strip(v2)," ,") > 0;
run;

And please supply your example data in the future in manner like above (data step with datalines), so we immediately have something to develop and test code against without having to make guesses about variable attributes and content.

sbxkoenk
SAS Super FREQ

Hello,

 

data have;
infile cards delimiter='|';
input V1 $ V2 $;
cards;
 	        |a1
a1, b1	    |a1
b2, b3, c6	|b2
c4, b2	    |b2
BO	        |a1
;
run;

data want;
 set have;
 Match = (INDEX(trim(left(upcase(V1))), trim(left(upcase(V2)))) gt 0);
run;
/* end of program */

Most functions have modifiers as optional additional arguments to make the function case insensitive, get rid of leading and/or trailing blanks in the comparison and so on.
I haven't checked in the doc if this is the case for the INDEX function so I do trim(left(upcase())).

 

Cheers,

Koen

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
  • 2 replies
  • 543 views
  • 1 like
  • 3 in conversation