DATA Step, Macro, Functions and more

check two strings for occurances of a value using index or?

Accepted Solution Solved
Reply
Regular Contributor
Posts: 182
Accepted Solution

check two strings for occurances of a value using index or?

Hi,

 

I need to check tow strings for occurances of  blank, 'U', and 'UU'.  If both strings contain, then newvar ='Unknown'.

 

I tried using find and Index...both getting error.  Can someone please tell me how to write this correctly?

 

if find(mhdxm_1,' ','U','UU')>0 and find(mhdxy_2,' ','U','UU')>0 then mhendtc='UNKNOWN';

 

Thanks!

 


Accepted Solutions
Solution
‎03-07-2018 11:35 AM
Super User
Posts: 6,785

Re: check two strings for occurances of a value using index or?

[ Edited ]

Is there any real reason to complicate this logic with the use of FIND?  Unless you have values that are right-hand-justified, you can simply use:

 

if mhdxm_1 in (' ' 'U' 'UU') and mhdxm_2 in (' ' 'U' 'UU') then mhendtc='UNKNOWN';

 

 Are things actually more complex than that?

View solution in original post


All Replies
Super User
Posts: 13,583

Re: check two strings for occurances of a value using index or?

Find (or index in your previously deleted post) can only search for one target string at a time.

Your really should post code with errors from the log to show the details.

 

Note that unless you are missing an additional rule anything that finds 'U' doesn't need to search for 'UU' as the singe U will be found in UU. If there were a third different string such a 'RW' then a third search would be needed.

 

find(mhdxm_1,' ','U','UU')>0 should be

 

(find(mhdxm_1,' ',)>0 OR find(mhdxm_1,'U')>0)

exercise for the interested reader to complete the search of the other variable.

Valued Guide
Posts: 597

Re: check two strings for occurances of a value using index or?

[ Edited ]

Your second argument has to be what your looking for and FIND function can search only one instance.

Try this:

if (find(mhdxm_1,'U','i')>0 and find(mhdxy_2,'U','i')>0) OR (find(mhdxm_1,'UU','i')>0 and find(mhdxy_2,'UU','i')>0) then mhendtc='UNKNOWN';

 

OR 

use prxmatch()

prxmatch("m/U|UU/oi,mhdxm_1)>0 or prxmatch("m/U|UU/oi,mhdxm_2)>0

Thanks,
Suryakiran
Solution
‎03-07-2018 11:35 AM
Super User
Posts: 6,785

Re: check two strings for occurances of a value using index or?

[ Edited ]

Is there any real reason to complicate this logic with the use of FIND?  Unless you have values that are right-hand-justified, you can simply use:

 

if mhdxm_1 in (' ' 'U' 'UU') and mhdxm_2 in (' ' 'U' 'UU') then mhendtc='UNKNOWN';

 

 Are things actually more complex than that?

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 100 views
  • 0 likes
  • 4 in conversation