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

I am trying to use the find function in a case where both string parameters passed to it contain double quotes.

In doing this, I discovered the following problem: Passing a TRIM'ed string to the FIND function is not sufficient to make it work (the c1 variable below is wrong). However using the trim option ('t') of the FIND function will give the desired results (the c2 variable below is correct).

Can someone explain to me why this is happening?

Code:

data test;

a = "lalala""example""lalala";

b = trim(cat("""","example",""""));

c1= find(a,b,'t');

c2 = find(a,b);

Result:

a = lalala"example"lalala

b = "example"

c1 = 7 (this is correct)

c2 = 0 (this is wrong)


1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

The result of the TRIM function must be used immediately. As soon as you assign it to a SAS character variable, it gets padded with blanks to the fixed length of that variable.

PG

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

The modifier 't' in the Find function trims trailing blanks from both a and b.

Without the modifier 't', then b is 200 characters in length and has many trailing blanks. So at no point does a match b because of the presence of the trailing blanks.

So if you try

c3=find(a,trim(b));

you will find that C3 = 7

--
Paige Miller
jakestiles
Calcite | Level 5


Hi Paige,

Thanks for the quick response. That is indeed a way to use the TRIM function instead of the FIND fuction's trim option to get the desired result.

I'm still at a loss for why it is necessary to trim the variable b again since I already trim'd it as I was creating it in the third line of code. Can anyone explain that?

Thanks,

Jake

ballardw
Super User


The length of the variable created by CAT is 200 UNLESS previously specified. You trimmed the VALUE but placed into a 200 character length storage. Hence the trailing blanks.

Try this:

c3 = find(a,trim(b));

as well.

PGStats
Opal | Level 21

The result of the TRIM function must be used immediately. As soon as you assign it to a SAS character variable, it gets padded with blanks to the fixed length of that variable.

PG
Tom
Super User Tom
Super User

SAS stores character strings as fixed length.  So B=TRIM(B) has no effect since SAS must add all of the spaces back onto the value when it writes it into the variable B.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 785 views
  • 4 likes
  • 5 in conversation