DATA Step, Macro, Functions and more

Multicharacter string in Scan function

Reply
New Contributor
Posts: 2

Multicharacter string in Scan function

hi

 

Im trying to parse my string into 3 parts, and the delimiter is "//". Example of my string:

ItemA=Andy//Male//Cat. However, I have some strings that have "/" as part of the string, and I'm unable to parse them properly, e.g.:

Bac/h//Male//Dog

 

I've used the following function but it doesnt parse properly. I've tried to replace Q with M but it doesnt work too.Appreciate any help pls. Thanks.

 

name = scan(ItemA,1,"//",'Q');

gender =scan(ItemA,2,"//",'Q');

animal =scan(ItemA,2,"//",'Q');

 

 

Super User
Posts: 9,548

Re: Multicharacter string in Scan function

A very clever solution to this kind of problem (multi-byte delimiters) can be found here: https://communities.sas.com/t5/Base-SAS-Programming/Handling-multi-character-word-delimiters/td-p/17...

 

Note that this is the first result of a google search for "sas scan multi-character delimiters".

See Maxim 6.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 226

Re: Multicharacter string in Scan function

@catisgar:

If you know some character that certainly does not occur in your string, you can use that as a substitute delimiter, here is an example using a hexadecimal NULL character, which is often a good candidate for this kind of stuff, as it is used as an end-of-string marker in C:

data x;
  ItemA='Bac/h//Male//Dog';
  str=tranwrd(ItemA,'//','00'x);
  name = scan(str,1,'00'x);
  gender =scan(str,2,'00'x);
  animal =scan(str,3,'00'x);
run;
Super User
Posts: 10,609

Re: Multicharacter string in Scan function

Here is another one.


data x;
  ItemA='Bac/h//Male//Dog';

temp=cats(ItemA,'//');
p=find(ItemA,'//');
if p=0 then do;want=ItemA; output;end;
 else do;
  do while(not missing(temp));
	p=find(temp,'//');
	want=substr(temp,1,p-1);
	output;
	temp=substr(temp,p+2);
  end;
 end;
run;

Super User
Posts: 12,994

Re: Multicharacter string in Scan function


catisgar wrote:

hi

 

Im trying to parse my string into 3 parts, and the delimiter is "//". Example of my string:

ItemA=Andy//Male//Cat. However, I have some strings that have "/" as part of the string, and I'm unable to parse them properly, e.g.:

Bac/h//Male//Dog

 

I've used the following function but it doesnt parse properly. I've tried to replace Q with M but it doesnt work too.Appreciate any help pls. Thanks.

 

name = scan(ItemA,1,"//",'Q');

gender =scan(ItemA,2,"//",'Q');

animal =scan(ItemA,2,"//",'Q');

 

 


Show an example of "doesn't work" and what the expected result should be.

This seems to do what you request:

data example;
   ItemA="Smith, John//Male//Dog";
   name = scan(ItemA,1,"//");
   gender =scan(ItemA,2,"//");
   animal =scan(ItemA,3,"//");
run;

By the way, your example for animal requesting the second item as the same for the gender "wouldn't work".

 

Super User
Posts: 9,548

Re: Multicharacter string in Scan function

@ballardw: // does not work as expected as a separator in scan(). Just run this slight modification of your code:

data example;
   ItemA="Smith/ John//Male//Dog";
   name = scan(ItemA,1,"//");
   gender =scan(ItemA,2,"//");
   animal =scan(ItemA,3,"//");
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 12,994

Re: Multicharacter string in Scan function

Posted in reply to KurtBremser

KurtBremser wrote:

@ballardw: // does not work as expected as a separator in scan(). Just run this slight modification of your code:

data example;
   ItemA="Smith/ John//Male//Dog";
   name = scan(ItemA,1,"//");
   gender =scan(ItemA,2,"//");
   animal =scan(ItemA,3,"//");
run;

@KurtBremser you're correct, I missed the OP  "sometimes" missing part of the delimeter.

 

Super User
Super User
Posts: 7,844

Re: Multicharacter string in Scan function

[ Edited ]

SCAN() does not support using multiple character strings as a delimiter.  But the INFILE statement does.

data want ;
  length name gender animal $20 ;
  infile cards dlmstr='//' truncover ;
  input name gender animal;
cards;
Andy//Male//Cat
Bac/h//Male//Dog
;

So if you already have the string in a dataset you could try using the "_infile_ trick" to let you use the INPUT statement to read from your character variable.

data want ;
  set have ;
  length name gender animal $20 ;
  infile cards dlmstr='//' truncover ;
  input @@;
  _infile_=itema;
  input @1 name gender animal @@;
cards;
Ignore me
;

 

Ask a Question
Discussion stats
  • 7 replies
  • 202 views
  • 4 likes
  • 6 in conversation