DATA Step, Macro, Functions and more

SAS Programming (Comparing two character variable)

Reply
Contributor
Posts: 35

SAS Programming (Comparing two character variable)

Hello Everyone,

I stuck  on one of the point.please help me out Smiley Frustrated

Scenario is We have two different variable in a dataset(Have) just like below

A      B     C

AA    w   a,b,c,d

BB   1     1,0,Null

CC  aa    AA,BBB,CCCC

DD   ab   a,b,c,d

 

I need a dataset (Want) as if the B varibale is part of c variable then create a flag as D and mark that as 1 else o 

 

A      B     C                           D

AA    w   a,b,c,d                    0

BB   1     1,0,Null                   1

CC  aa    AA,BBB,CCCC        1

DD   ab   a,b,c,d                    0

 

 

 

 

Super User
Super User
Posts: 7,942

Re: SAS Programming (Comparing two character variable)

Hi,

 

Well, no code as not typing in that test data.  You can check if the text appears in the string something like:

if index(c,b)>0 then d=1;

else d=0;

 

However your row CC, it is not logical the text "aa" is not found in the other string.  Do you have another rule you didn't mention which allow upper case to equal lower case, if so:

if index(upcase(c),upcase(b))>0 then d=1;

else d=0;

 

Super User
Posts: 5,498

Re: SAS Programming (Comparing two character variable)

Here are a couple of choices.

 

if findw(trim(C), trim(B), ',') > 0 then d=1;

else d=0;

 

if findw(trim(C), trim(B), ',', 'i') > 0 then d=1;

else d=0;

 

The "i" option will ignore upper vs. lower case, and would return 1 for the third observation.

 

Note that there are pitfalls that have to be addressed.  Since B takes on different lengths, I would assume you want to ignore trailing blanks that are part of B.  And INDEX searches for strings, not words.  So this comparison is true:

 

if index('abc,def', 'b') > 0 then D=1;

Ask a Question
Discussion stats
  • 2 replies
  • 184 views
  • 0 likes
  • 3 in conversation