BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HeatherNewton
Quartz | Level 8
options nodate nostimer ls=78 ps=60;
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;

I dont understand the answer, why is q=0 and w=10? 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

@HeatherNewton  - If the answers fix your problem, it's good practice to update the post as answered 🙂.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Always keep in mind that character variables are padded to their defined length with blanks, and in the first call of INDEX, those blanks are there in variable b and would need to also be present in a (which they are not, of course).

In the second call to INDEX, the TRIM function removes the trailing blanks, and the remaining string is found.

PeterClemmensen
Tourmaline | Level 20

This is Example 2 from the Index Function Documentation.

 

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;

 

What you want to notice here is the Length Statement, giving both a and b a length of $14. Meaning that SAS pads both a and b with trailing blanks. The code below gives the same as above without the Length Statement. 

 

data _null_;
   a='ABC.DEF (X=Y) ';
   b='X=Y           ';
   q=index(a, b);
   w=index(a, trim(b));
   put q= w=;
run;

This means that in index(a, b) looks for the string 'X=Y           ' (including trailing blanks) in the string 'ABC.DEF (X=Y) '. This returns zero because it's not there. Index(a, trim(b)) looks for 'X=Y' (without trailing blanks) in the same string. This returns 10. 

PaigeMiller
Diamond | Level 26

Variable B has length 14. It has the value X=Y plus 11 blanks. This string of X=Y followed by 11 blanks does not appear in A.

 

When you do the TRIM then the value is X=Y without any trailing blanks, which does appear in A.

 

To fix this use

 

length a $14;

or even remove the LENGTH statement entirely as it is not needed in this data set.

--
Paige Miller
Patrick
Opal | Level 21

The secret is hidden in the length statement.

Patrick_0-1646650688335.png

Because this length statement defines b with a length of $14 the index() function will use the fully expanded value as below (blanks are also just characters):

Patrick_1-1646650809648.png

Because this is not a match the index() function returns a value of zero.

 

If you use the trim function then the index() function will see the following (trailing blanks removed):

Patrick_3-1646650960406.png

Now it's a match and that's why the index() function returns a value of 10.

 

Does that explain it to you?

 

 

HeatherNewton
Quartz | Level 8
Yes thank you all for the clear explanation!
SASKiwi
PROC Star

@HeatherNewton  - If the answers fix your problem, it's good practice to update the post as answered 🙂.

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
  • 6 replies
  • 829 views
  • 2 likes
  • 6 in conversation