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

I have a program with a list of strings delimited by "|" that I want to look up in a string variable column. I'm getting some odd behavior that I don't understand. Below is a code example:

data birds;

	/* list of words */
	all_the_words = 'BIRD|DOG|CAT';
	length word_from_list $100.;

	/* pull the first word from all_the_words */
	do i = 1 to 1;
		word_from_list = strip(scan(all_the_words, i, '|'));
	end;

	put "word_from_list: " word_from_list;

	/* assert that word_from_list is the same */
	words_the_same = (word_from_list = "BIRD");
	
	/* text in which we look up BIRD */
	text_for_lookup = "BIRD (text here)";

	/* find results for word_from_list */
	found_it = find(text_for_lookup, word_from_list);

	/* find results for literal "BIRD" */
	found_it_2 = find(text_for_lookup, "BIRD");

run;

 

I'm finding that despite words_the_same being 1, I'm getting different results for found_it and found_it_2. Attached is the output dataset from this code run. I'm curious why these give different results and how to make it so that word_from_list gets found.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  When I used this modification of your code, I got the same result for both finds:

Cynthia_sas_0-1719508581251.png

With the literal string you bounded the string to 4 characters for the comparison. However you defined WORD_FROM_LIST to have a length of 100 and so even though you don't see it in the LOG, the variable WORD_FROM_LIST is padded out to the full length and BIRD in the TEXT_FOR_LOOKUP variable is not equal to the variable WORD_FROM_LIST because of the trailing spaces.

Cynthia

 

View solution in original post

3 REPLIES 3
Cynthia_sas
SAS Super FREQ

Hi:

  When I used this modification of your code, I got the same result for both finds:

Cynthia_sas_0-1719508581251.png

With the literal string you bounded the string to 4 characters for the comparison. However you defined WORD_FROM_LIST to have a length of 100 and so even though you don't see it in the LOG, the variable WORD_FROM_LIST is padded out to the full length and BIRD in the TEXT_FOR_LOOKUP variable is not equal to the variable WORD_FROM_LIST because of the trailing spaces.

Cynthia

 

JStein
Calcite | Level 5

Thank you! 

Tom
Super User Tom
Super User

SAS character variables are fixed length (padded with spaces).  So make sure that you don't include the spaces in the search term.  FIND() has an option to do that for you.

	found_it = find(text_for_lookup, word_from_list, 't');

Also there is no need to remove trailing spaces before assigning a value to a variable. SAS will just add them back to fill out the variable.  So there is no need to use the STRIP() function.  Instead just use LEFT() to remove leading spaces.

    word_from_list = left(scan(all_the_words, i, '|'));

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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