BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
bn820
Obsidian | Level 7

The FINDW function is returning smaller count of observations compared to the dataset. e.g) The code returns a freq of 5 for adherence but the actual count is 8 in the dataset.  Below is my code:

IF FINDW (upcase(SocialEmotinalStress),upcase('Unstable Housing'))>0 THEN BS_Housing=1; ELSE BS_Housing=0;
IF FINDW (SocialEmotinalStress,'Adherence')>0 THEN BS_Adherence=1; ELSE BS_Adherence=0;
IF FINDW (SocialEmotinalStress,'Chronic Disease')>0 THEN BS_Chronic=1; ELSE BS_Chronic=0;
IF FINDW (SocialEmotinalStress,'Social Support/Isolation')>0 THEN BS_Social=1; ELSE BS_Social=0;
IF FINDW (SocialEmotinalStress,'Lack of Access/Financial Resources')>0 THEN BS_Access=1; ELSE BS_Access=0;
IF FINDW (upcase(SocialEmotinalStress),upcase('Medical Condition'))>0 THEN BS_Medical=1; ELSE BS_Medical=0;
IF FINDW (SocialEmotinalStress,'Mental Health Conditions')>0 THEN BS_Mental=1; ELSE BS_Mental=0;
IF FINDW (SocialEmotinalStress,'Other')>0 THEN BS_Other1=1; ELSE BS_Other1=0;
IF FINDW (SocialEmotinalStress,'Trauma')>0 THEN BS_Trauma=1; ELSE BS_Trauma=0;
IF FINDW (SocialEmotinalStress,'Stressor')>0 THEN BS_Stressor=1; ELSE BS_Stressor=0;
IF FINDW (SocialEmotinalStress,'Violence and intimate partner violence (IPV)')>0 THEN BS_IPV=1; ELSE BS_IPV=0;

1 ACCEPTED SOLUTION

Accepted Solutions
9 REPLIES 9
PaigeMiller
Diamond | Level 26

Please share a portion of the data you are using, along with the desired output for this portion of the data. Please provide the data as working SAS data step code (examples and instructions); and do not provide the data in other forms.

--
Paige Miller
Reeza
Super User
I notice you use upcase in the first one, but not in the second. Have you confirmed the case and spelling matches exactly? Are you aware of the it modifiers for the FINDW() function?
You may also want to consider an array instead. It will keep the code much cleaner and easier to follow.
SASKiwi
PROC Star

You are searching for multiple word matches but using the single word function search FINDW. Try FIND or INDEX instead.

Tom
Super User Tom
Super User

@SASKiwi wrote:

You are searching for multiple word matches but using the single word function search FINDW. Try FIND or INDEX instead.


FINDW works fine for finding multiple word matches.  As long as the words are in the same order with the separation between the words in the string being searched as they appear in the strings being looked for.

 

So 

 

findw('a b c d e','b c',' ')

Will find 'b c' in 'a b c d e'.

 

But 

findw('a b c d e','b    c',' ')

Will not match.

 

Reeza
Super User
array _words(3) _temporary_ $50. ('Unstable Housing', 'Adherence', 'Chronic Disease'); *Fill in rest of terms;

array _BS(3) BS_housing bs_adherence bs_chronic; *fill in rest of variables;

do i=1 to dim(_words);
IF FIND (SocialEmotinalStress, _words(i), 'it')>0 THEN _bs(i)=1; ELSE _bs(i)=0;
end;

FIND documentation

 

Modifiers (i/t)

i or I

ignores character case during the search. If this modifier is not specified, FIND searches only for character substrings with the same case as the characters in substring.

t or T

trims trailing blanks from string and substring.

 

Edited to FIND based on @SASKiwi answer.

 


@bn820 wrote:

The FINDW function is returning smaller count of observations compared to the dataset. e.g) The code returns a freq of 5 for adherence but the actual count is 8 in the dataset.  Below is my code:

IF FINDW (upcase(SocialEmotinalStress),upcase('Unstable Housing'))>0 THEN BS_Housing=1; ELSE BS_Housing=0;
IF FINDW (SocialEmotinalStress,'Adherence')>0 THEN BS_Adherence=1; ELSE BS_Adherence=0;
IF FINDW (SocialEmotinalStress,'Chronic Disease')>0 THEN BS_Chronic=1; ELSE BS_Chronic=0;
IF FINDW (SocialEmotinalStress,'Social Support/Isolation')>0 THEN BS_Social=1; ELSE BS_Social=0;
IF FINDW (SocialEmotinalStress,'Lack of Access/Financial Resources')>0 THEN BS_Access=1; ELSE BS_Access=0;
IF FINDW (upcase(SocialEmotinalStress),upcase('Medical Condition'))>0 THEN BS_Medical=1; ELSE BS_Medical=0;
IF FINDW (SocialEmotinalStress,'Mental Health Conditions')>0 THEN BS_Mental=1; ELSE BS_Mental=0;
IF FINDW (SocialEmotinalStress,'Other')>0 THEN BS_Other1=1; ELSE BS_Other1=0;
IF FINDW (SocialEmotinalStress,'Trauma')>0 THEN BS_Trauma=1; ELSE BS_Trauma=0;
IF FINDW (SocialEmotinalStress,'Stressor')>0 THEN BS_Stressor=1; ELSE BS_Stressor=0;
IF FINDW (SocialEmotinalStress,'Violence and intimate partner violence (IPV)')>0 THEN BS_IPV=1; ELSE BS_IPV=0;


 

bn820
Obsidian | Level 7

Thanks everyone! These were vryhelpful

SASKiwi
PROC Star

@bn820 - Please update your post as answered in that case.

bn820
Obsidian | Level 7

Sorry. I am new to SAS community. I am still learning.

Reeza
Super User
You usually mark the solution that is the answer, especially if there are multiple answers,.

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
  • 9 replies
  • 850 views
  • 7 likes
  • 5 in conversation