DATA Step, Macro, Functions and more

can i define double space as a separator in sas scan function

Accepted Solution Solved
Reply
SAS Employee
Posts: 7
Accepted Solution

can i define double space as a separator in sas scan function

Hi,

 

I have a field that have two names in it, separated by two-spaces. how do I get only the second person from this field?

 

examples:

 

---1st person--------  -----2nd person------

LUKASZ STASIAK  ANNA KAROWIEC

            ^1 space    ^2

 

-----------1st person---------  ------------2nd person---------

ADAM KAPEC KOWALKI  JURAND ZE SPYCHOWA

         ^1           ^1              ^2             ^1  ^1

 

 

 

this:

sender   = scan(tpp_name, 1, " ");

receiver = scan(tpp_name, 2, " ");

 

treats the "  " as <space separator><space separator> not <double space separator>. Is there any workaround for that?

 

Thanks

Lukasz


Accepted Solutions
Solution
‎07-21-2016 03:47 AM
Super User
Posts: 11,343

Re: can i define double space as a separator in sas scan function

Posted in reply to LukaszStasiak

Work around would likely involve the INDEX function which returns the start position of one string within another or zero if not found.

 

pos = index(tpp_name ,'  ');

sender = substr(tpp_name ,1,pos-1);

receiver =  substr(tpp_name ,pos+2);

 

should work but you should set the lengths of the sender and receiver varaibles before use.

View solution in original post


All Replies
Solution
‎07-21-2016 03:47 AM
Super User
Posts: 11,343

Re: can i define double space as a separator in sas scan function

Posted in reply to LukaszStasiak

Work around would likely involve the INDEX function which returns the start position of one string within another or zero if not found.

 

pos = index(tpp_name ,'  ');

sender = substr(tpp_name ,1,pos-1);

receiver =  substr(tpp_name ,pos+2);

 

should work but you should set the lengths of the sender and receiver varaibles before use.

Super User
Posts: 10,020

Re: can i define double space as a separator in sas scan function

Posted in reply to LukaszStasiak

Check modification 'M':

 

scan(tpp_name, 2, " ",'m');

SAS Employee
Posts: 7

Re: can i define double space as a separator in sas scan function

doesn't work as intended - it returns the second word from the string 

SAS Super FREQ
Posts: 8,862

Re: can i define double space as a separator in sas scan function

Posted in reply to LukaszStasiak

Hi:

  Both FIND and INDEX worked for me.

cynthia

use_substr_with_index.png

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 416 views
  • 1 like
  • 4 in conversation