Help using Base SAS procedures

scan function. Part of third param("charlist") exist in first parameter.

Reply
Contributor
Posts: 26

scan function. Part of third param("charlist") exist in first parameter.

Hi all,
So I have small task: divide string into few sub-strings, using Delimiter ="]}".
But there are few "]" and "}" in source string - scan function treat each of this chars as delimeter and return wrong result...
Example:

%macro divideIntoSepareteJsons;

%let str1="typ}es": ]} "LAYO]UT":]} "Label": "LAYOUT";
%let str2=%bquote(&str1);
%let delim=%bquote(]});

data test;
txt="&str2";
del="&delim";
firstword=scan(txt,1,del);
secword=scan(txt,2,del);
thirdword=scan(txt,3,del);
forword=scan(txt,4,del);
;
run;
%mend;


%divideIntoSepareteJsons

So results must be:
firstword="typ}es":;
secword="LAYO]UT":
thirdword="Label": "LAYOUT"
forword=

But , becouse we have "}" in first sub-string("typ}es"Smiley Happy we have such result:
firstword="typ
secword=es":;
thirdword="LAYO]UT":
forword="Label": "LAYOUT"

So I must somehow said SAS that I look only for whole "]}" as delimiter, not for 2 separate delimiters...

Thanks in advanced for your help...
Super Contributor
Super Contributor
Posts: 3,174

Re: scan function. Part of third param("charlist") exist in first parameter.

Posted in reply to Yuriy2301
Pre-process the SAS variable and use the TRANWRD function to change the "delimiter word" to some unique single-character. Then you can use the SCAN function, as needed.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 26

Re: scan function. Part of third param("charlist") exist in first parameter.

Thanks Scott,
I thought about such solution before,
but we can't be sure that this one unique single-character delimiter will not appear in sub-words...
When we have complicated delimiter,for example [{{{] it's much more safer then use only one delimeter symbol(even if it is "~" or smth. like this)...

So I look for possibility to use some complex delimiter, if it's possible at all...

But, anyway, thanks!
Contributor
Posts: 26

Re: scan function. Part of third param("charlist") exist in first parameter.

Posted in reply to Yuriy2301
One more detail:
We also can make some list of single delimiters, for example:
~,*,^...
Then we at beginning will check input string if such single delimiter exists in it.
If not exists-we can use this solution with tranwrd and replacing complex delim. into single one...
It looks like good variant,
but even in such case we will have one additional scanning, for finding if single delimiter exist in string, and it's not very optimalSmiley Sad

I feelSmiley Happy that must be some simpler solution, it's looks so simle task...
Super Contributor
Super Contributor
Posts: 3,174

Re: scan function. Part of third param("charlist") exist in first parameter.

Posted in reply to Yuriy2301
Okay - suggest you use a hex-data string then, coded as a literal, like "00"X. And you could use the INDEXC function in an assignment statement first, to detect if the character is or is not present, and then use it if not present in your data.

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 634

Re: scan function. Part of third param("charlist") exist in first parameter.

Take a look at the Perl Regular Expression functions. PRXxxxxx They allow a great deal of flexibility (and complexity), but can be very helpful in tight corners and dark alleys.
Contributor
Posts: 26

Re: scan function. Part of third param("charlist") exist in first parameter.

Thanks!
Respected Advisor
Posts: 3,799

Re: scan function. Part of third param("charlist") exist in first parameter.

Posted in reply to Yuriy2301
SCAN function with Q option.

[pre]
data _null_;
text='"typ}es": ]} "LAYO]UT":]} "Label": "LAYOUT"';
w = countw(text,']}','Q');
do i = 1 to w;
word = scanq(text,i,']}','Q');
put 'NOTE: ' i= word=;
end;
run;
[/pre]


[pre]
NOTE: i=1 word="typ}es":
NOTE: i=2 word="LAYO]UT":
NOTE: i=3 word="Label": "LAYOUT"
[/pre]
Regular Contributor
Posts: 241

Re: scan function. Part of third param("charlist") exist in first parameter.

Posted in reply to data_null__
The reason why data _null_'s solution is working for the test string is nothing to do with multi-character delimiters. This works because Q option ignores any delimiter characters inside quoted string; and because scan, by default, does not return (or count) 'null' words -- meaning consecutive delimiters are considered as one.

See my sas-l post for a true multi-character delimiter support via a variant of coding techniques known as "_infile_ magic":
http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1010d&L=sas-l&D=1&P=26236

Or toby's macro relying on call prxnext with a cleaver use of non-greedy modifier(*?) to the perl reg ex wild card character (.) at:
http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1010d&L=sas-l&D=1&O=D&P=27264
Contributor
Posts: 26

Re: scan function. Part of third param("charlist") exist in first parameter.

Posted in reply to chang_y_chung_hotmail_com
Thanks!
Contributor
Posts: 26

Re: scan function. Part of third param("charlist") exist in first parameter.

Posted in reply to data_null__
Thanks, great!
Ask a Question
Discussion stats
  • 10 replies
  • 702 views
  • 0 likes
  • 5 in conversation