DATA Step, Macro, Functions and more

Index Function

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

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


Accepted Solutions
Solution
‎12-26-2012 11:20 AM
PROC Star
Posts: 7,363

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.

View solution in original post


All Replies
Solution
‎12-26-2012 11:20 AM
PROC Star
Posts: 7,363

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.

Super Contributor
Posts: 1,040

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

Contributor
Posts: 56

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

Super Contributor
Posts: 1,040

Re: Index Function

Hi,

I appreciate your valuable sugestions in this regard.

Thanks

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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