BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GRV
Calcite | Level 5 GRV
Calcite | Level 5


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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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, ' ' )

    ) ;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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, ' ' )

    ) ;

GRV
Calcite | Level 5 GRV
Calcite | Level 5

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 3652 views
  • 3 likes
  • 2 in conversation