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)
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.
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
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
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.
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.
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.
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!
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.