BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11

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 

Blue Blue
7 REPLIES 7
Tom
Super User Tom
Super User

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.

ChrisNZ
Tourmaline | Level 20

> b doesn't have spaces that trim b takes that out

 

Yes it does. Remove the LENGTH statement and it won't.

Kurt_Bremser
Super User

@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.

ballardw
Super User

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.

 

GN0001
Barite | Level 11

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

 

 

Blue Blue
SASKiwi
PROC Star

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

INDEX Function

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 876 views
  • 3 likes
  • 6 in conversation