Hi Pals,
Below is a peice of code i am trying to understand Index function. according to me both are almost same code and should return the same result, but that is not the case. Could anybody help me understand the working of the code.
data _null_;
length a b $14;
a='ABC.DEF (X=Y)';
b='X=Y';
q=index(a,b);
put q=;
run;
*proc contents data=test3 ;
*run;
data _null_;
a = 'ABC.DEF (X=Y)';
b = 'X=Y';
x = index(a,b);
put x=;
run;
The documentation is just one click away!
Functions and CALL Routines: INDEX Function - 9.2
Both leading and trailing spaces are considered part of the excerpt argument. To remove trailing spaces, include the TRIM function with the excerpt variable inside the INDEX function. |
The string 'X=Y ' is not found in variable A.
Hi Chris,
Thanks for the quick response.
But, If we see both the data step code I have written and value in variable a and b are same. The second one gives me correct answer but the first one does not.
I would like to understand this behavior why the second result is giving me correct answer and first one is not. If I comment the length code in first datastep I get the correct answer , but I am not able to understand the logic behind this.
Thanks.
@ChrisNZ already explained why the index function in the first step fails to find the string: the variable b is always 14 chars long and padded with blanks, not matter what value is assigned to it. If you don't use length (or attrib) to define a variable, its length is automatically set depending on the first use.
Thanks Chris and Andrea for answering the question ... It makes complete sense now ...appreciate the help in clearing the doubt.
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 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.