Sas function substr..how to use it as Pseudo variable


#1

Please explain this example
how before and after are coming in the output?


#2

Hi @sanchi_singh1,

Ok, I will try to start with the basics -

  • Index Function - It returns the position in a string where the name specified is found. Say Index(Name, ‘Ratface’) will return where Ratface is positioned in the string. If Ratface is not found index function will return zero(0) as the output.
  • Substr Function - It basically replaces the string. Syntax : Substr(x,y,z) = ‘Some String’ , where x = variable in which you want to make a replacement, y=position from where you want to replace the string, z= length of string which is going to be replaced from the right of position after inserting the designated string.

In the example given -

  • Case 1: Duke Owen doesn’t contain ‘Ratface’ string , so index function will return Where = 0, now if where = 0, then the name will be trimmed and ‘,Evil Genius’ will be concatenated. That’s how Duke Owen becomes Duke Owen, Evil Genius

  • Case 2: In this case index function founds Ratface in the string, so it returns the position which is 6, so where = 6, as we can see where is not equal to zero the substr function will come in play. Now Substr function becomes substr(Name, 6, 13) = ‘The Powerful’. So previously the name was Duke Ratface Owen and now it has become Duke the powerful , replacing the string from 6th position to The Powerful and replacing 13 characters from 6th position, hence omitting Owen.

Hope this helps.

Regards,
Aayush


#3

Thanku sir was a great help :smile:
regards