Quartz | Level 8

## Index Function

Could you please explain me what this little piece of code is doing?

I have difficulty understanding what the >0 is doing and also the -1???

data abc;

set xyz(drop=fmtname rename=(start=drg label=desc));

if index(desc,"W MCC")>0then GrpDsc=substr(desc,1,(index(desc,"W MCC")-1));

elseif index(desc,"W/O MCC")>0then GrpDsc=substr(desc,1,(index(desc,"W/O MCC")-1));

elseif index(desc,"W CC")>0then GrpDsc=substr(desc,1,(index(desc,"W CC")-1));

elseif index(desc,"W/O CC/MCC")>0then GrpDsc=substr(desc,1,(index(desc,"W/O CC/MCC")-1));

else
GrpDsc=desc;

run;

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## Re: Index Function

Karun,  You can answer such questions by simply reading the documentation.  The 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."

Thus, if it returns a number greater than 0 it found the string starting at the returned location.

Since substr, in turn, extracts characters starting from (in our case position 1), and extracts X characters (in your case the second value), it is extracting up to but not including the found string.

4 REPLIES 4
Opal | Level 21

## Re: Index Function

Karun,  You can answer such questions by simply reading the documentation.  The 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."

Thus, if it returns a number greater than 0 it found the string starting at the returned location.

Since substr, in turn, extracts characters starting from (in our case position 1), and extracts X characters (in your case the second value), it is extracting up to but not including the found string.

Quartz | Level 8

## Re: Index Function

Hi

Thanks for the detailed explanation. I scanned through the documentation and may be I didnt check the right page. I will make it a habit.

Thanks

Quartz | Level 8

## Re: Index Function

Karun,

Just like Arthur suggested, it would be best for you to research on the below link looking for SAS Functions and call routines. Ideally, a discussion on application orientated medium to complex queries is rather interestingly appreciated than merely asking answers on learning resources. Merry Christmas.

Cheers from England,

Andy

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212242.htm

Quartz | Level 8

## Re: Index Function

Hi,

I appreciate your valuable sugestions in this regard.

Thanks

Discussion stats
• 4 replies
• 1176 views
• 0 likes
• 3 in conversation