Hello team
why q is equal to 0.
b = 'x=y' trim(b) will be same, b doesn't have spaces that trim b takes that out!!
data _null_; length a b $14; a='ABC.DEF (X=Y) '; b='X=Y'; q=index(a, b); w=index(a, trim(b)); put q= w=; run; q=0 and w=10 /*log shows it*/
thanks for the response. b&b
SAS uses FIXED LENGTH character variables.
You defined B to hold 14 bytes. You assigned a value that was only 3 bytes long, so the other 11 bytes were filled with spaces.
> b doesn't have spaces that trim b takes that out
Yes it does. Remove the LENGTH statement and it won't.
@GN0001 wrote:
trim(b) will be same, b doesn't have spaces
b DOES have spaces, because you defined it with a length of 14, so the remaining bytes have to be padded with blanks.
If you do not include b in the LENGTH statement, then the data step compiler will use the length of the assigned literal string to define the length of the variable.
Here is code that will demonstrate, in one way, the actual value of B for the first call to index.
data _null_; length a b $14; a='ABC.DEF (X=Y) '; b='X=Y'; z= quote(b); put z=; run;
Read the LOG and you will see that the variable Z has a bunch of blanks before the final quote. Which means those blanks are there unless removed.
Hello,
I thought index function finds the position of first character of second string in the first string. As soon as it find it, it returns the position. I didn't know that it checks all the second string in the first string, then it returns the first character position.
Thanks
blue & blue
I thought index function finds the position of first character of second string in the first string. As soon as it find it, it returns the position
Searches a character expression for a string of characters, and returns the position of the string's first character for the first occurrence of the string.
If you read the description of the INDEX function as copied from the documentation then it should be clear how it works - it is trying to find the whole of the second string in the first one.
See Maxim 46 and always keep its consequences in mind.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.