BookmarkSubscribeRSS Feed
catisgar
Calcite | Level 5

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');

 

 

7 REPLIES 7
s_lassen
Meteorite | Level 14

@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;
Ksharp
Super User
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;

ballardw
Super User

@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".

 

Kurt_Bremser
Super User

@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;
ballardw
Super User

@Kurt_Bremser 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;

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

 

Tom
Super User Tom
Super User

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
;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 6042 views
  • 4 likes
  • 6 in conversation