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

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
art297
Opal | Level 21

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

4 REPLIES 4
art297
Opal | Level 21

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.

robertrao
Quartz | Level 8

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

Andygray
Quartz | Level 8

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

robertrao
Quartz | Level 8

Hi,

I appreciate your valuable sugestions in this regard.

Thanks

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 4 replies
  • 2159 views
  • 0 likes
  • 3 in conversation