BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

Lets say I have a dataset that produces two columns

 

 

ATTY                FIRM

Scott                 Scott and Wagner

Hampshire        Mills and Walker

Beasley            Jones and Beasley

 

In this case I want to have a flag that says if any part of the name DOES NOT MATCH (ATTY and FIRM)  then put a 'Y' in a flag called Choose, else do not choose it.  So in this case only Hampshire      Mills and Walker would get chosen.  Is there a way to do this?

 

ATTY                FIRM                                          choose

Scott                 Scott and Wagner

Hampshire        Mills and Walker                            Y

Beasley            Jones and Beasley

7 REPLIES 7
Kurt_Bremser
Super User

Use the findw() function:

data have;
infile cards dlm=',';
input atty :$30. firm :$30.;
cards;
Scott,Scott and Wagner
Hampshire,Mills and Walker
Beasley,Jones and Beasley
;
run;

data want;
set have;
choose = ifc(findw(firm,strip(atty)),' ','Y');
run;

Note the function that strips the trailing (and/or leading) blanks from atty.

Q1983
Lapis Lazuli | Level 10
Thanks for your input here. I was wondering why it did not work exactly as planned
data have;
infile cards dlm=',';
input atty :$30. firm :$30.;
cards;
Scott,Scott and Wagner
Hampshire,Mills and Walker
Beasley,Jones and Beasley
Beasley,Jones and Bonds
;
run;

data want;
set have;
choose = ifc(findw(firm,strip(atty)),' ','Y');
run;
For the last one Beasley,Jones and Bonds I would get a 'Y' because it see the 'B' which is a character match. Is there a way to force it to look at the first 2 or 3 characters. In this example I would want this last entry NOT to be a match
novinosrin
Tourmaline | Level 20

data have;
infile cards dlm=',';
input atty :$30. firm :$30.;
cards;
Scott,Scott and Wagner
Hampshire,Mills and Walker
Beasley,Jones and Beasley
;
run;


data want;
set have;
if not indexw(firm,strip(atty)) then choose='Y';
run;
Q1983
Lapis Lazuli | Level 10

Lets say the last entry  Beasley,Jones and Beasley  was Beasley,Jones and Bond instead.

I would still get a 'Y' for a match.

Is there a way to get this to look at the first 2 or 3 characters.  In that case the last entry would not be a match which is what I would want in this case. The code you provided is effective however if the first char is a match in either column we get a 'Y' which is not what I would want

novinosrin
Tourmaline | Level 20

Can you modify the sample input and the output for the sample plz

Q1983
Lapis Lazuli | Level 10

data have;

infile cards dlm=',';

input atty :$30. firm :$30.;

cards;

 

Scott,Scott and Wagner

Hampshire,Mills and Walker

Beasley,Jones and Bond

;

run;

 

 

data want;

set have;

if not indexw(firm,strip(atty)) then choose='Y';

run;

 

So in this case because in the last entry the "B" in Beasley happens to match the "B" in Bonds, we would get a 'Y'.  However is there a way to get the code to look at the first 2 or 3 char and apply the same logic.  In this case since Beasley is not Bond I would want this to not show a 'Y' for a match, currently it does

novinosrin
Tourmaline | Level 20

Hi @Q1983   The Y is case of non matches both my code and Kurt's. 

The result that you see below in the 2nd and 3rd record being 'Y' is a result of  the condition "not indexw" (note the not  or in other words false positives.   So basically, Y is a result of non-matches and not matches.

This meets your original question where you wrote ="In this case I want to have a flag that says if any part of the nameDOES NOT MATCH(ATTY and FIRM)  then put a 'Y' in a flag called Choose, else do not choose it" Kindly review

atty firm choose
Scott Scott and Wagner  
Hampshire Mills and Walker Y
Beasley Jones and Bond Y

 

 

 


@Q1983 wrote:

 

So in this case because in the last entry the "B" in Beasley happens to match the "B" in Bonds, we would get a 'Y'.  However is there a way to get the code to look at the first 2 or 3 char and apply the same logic.  In this case since Beasley is not Bond I would want this to not show a 'Y' for a match, currently it does


 

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
  • 7 replies
  • 623 views
  • 3 likes
  • 3 in conversation