BookmarkSubscribeRSS Feed
Yuriy2301
Calcite | Level 5
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":) 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...
10 REPLIES 10
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Yuriy2301
Calcite | Level 5
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!
Yuriy2301
Calcite | Level 5
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 optimal:(

I feel:) that must be some simpler solution, it's looks so simle task...
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
ArtC
Rhodochrosite | Level 12
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.
data_null__
Jade | Level 19
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]
chang_y_chung_hotmail_com
Obsidian | Level 7
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1949 views
  • 0 likes
  • 5 in conversation