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.
V1 | V2 | Match |
a1 | 0 | |
a1, b1 | a1 | 1 |
b2, b3, c6 | b2 | 1 |
c4, b2 | b2 | 1 |
BO | a1 | 0 |
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!
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.
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.
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: