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');
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.
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;
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;
@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".
@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 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.
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
;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.