Solved
New Contributor
Posts: 2

# SAS INDEX function not working properly. Can anyone tell why it is so?

I am running following code on Mainframe system and INDEX function is giving unexpected results.

DATA DATA01;

FILE OUTFIL ;

V = TRIM(LEFT(SCAN('  05 WAAA-ZZ     PIC S9(4) COMP.',2, ' ' ))) ;

I = INDEX(' MOVE WAAA-ZZ(1) TO WBBB-RESP-ZZ ', V ) ;

PUT V ;

PUT I ;

RUN ;

On output file it is showing

WAAA-ZZ   (i.e. value stored in V)

0    (i.e. value of I - which mean it has not found WAAA-ZZ in the given string, however we can see that it is present there)

Can anyone explain why it is behaving in this way?

Accepted Solutions
Solution
‎04-19-2013 09:04 AM
Super User
Posts: 7,860

## Re: SAS INDEX function not working properly. Can anyone tell why it is so?

You are placing the TRIM() function in the wrong place.  SAS stores character variables as fixed length strings padded with spaces.  So in the assignment statement SAS will just put the trailing spaces back onto the value of V to pad it out to fill the fixed length.

V = SCAN('  05 WAAA-ZZ     PIC S9(4) COMP.',2, ' ' ) ;

I = INDEX(' MOVE WAAA-ZZ(1) TO WBBB-RESP-ZZ ',  Trim(V) ) ;

or

I = INDEX(' MOVE WAAA-ZZ(1) TO WBBB-RESP-ZZ '

,SCAN('  05 WAAA-ZZ     PIC S9(4) COMP.',2, ' ' )

) ;

All Replies
Solution
‎04-19-2013 09:04 AM
Super User
Posts: 7,860

## Re: SAS INDEX function not working properly. Can anyone tell why it is so?

You are placing the TRIM() function in the wrong place.  SAS stores character variables as fixed length strings padded with spaces.  So in the assignment statement SAS will just put the trailing spaces back onto the value of V to pad it out to fill the fixed length.

V = SCAN('  05 WAAA-ZZ     PIC S9(4) COMP.',2, ' ' ) ;

I = INDEX(' MOVE WAAA-ZZ(1) TO WBBB-RESP-ZZ ',  Trim(V) ) ;

or

I = INDEX(' MOVE WAAA-ZZ(1) TO WBBB-RESP-ZZ '

,SCAN('  05 WAAA-ZZ     PIC S9(4) COMP.',2, ' ' )

) ;

New Contributor
Posts: 2

## Re: SAS INDEX function not working properly. Can anyone tell why it is so?

Thanks for your help Tom. It worked correctly when I placed the TRIM function in correct statement.

🔒 This topic is solved and locked.