Help using Base SAS procedures

Difference in functionality of TRIM function and FIND function's trim option

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Difference in functionality of TRIM function and FIND function's trim option

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)



Accepted Solutions
Solution
‎05-20-2015 04:23 PM
Respected Advisor
Posts: 4,646

Re: Difference in functionality of TRIM function and FIND function's trim option

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


All Replies
Trusted Advisor
Posts: 1,613

Re: Difference in functionality of TRIM function and FIND function's trim option

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

New Contributor
Posts: 2

Re: Difference in functionality of TRIM function and FIND function's trim option


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

Super User
Posts: 10,500

Re: Difference in functionality of TRIM function and FIND function's trim option


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.

Solution
‎05-20-2015 04:23 PM
Respected Advisor
Posts: 4,646

Re: Difference in functionality of TRIM function and FIND function's trim option

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
Super User
Super User
Posts: 6,500

Re: Difference in functionality of TRIM function and FIND function's trim option

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.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 255 views
  • 4 likes
  • 5 in conversation