Well, its a bit tricky in code, an example below. Also you run the risk of a few various, extra spaces, dots in words (for example etc., or e.g.). I would prefer to proces the text at source rather than at receipt.
data have;
string="Officials said they had no choice after more than 13,000 people entered the country since Hungary fenced off its border with Serbia earlier this week. Many have been taken by bus to reception centres but some say they plan to walk to neighbouring Slovenia? Huge numbers of people heading north from the Mediterranean have created a political crisis in the European Union? Croatian officials said roads leading to the border crossings had also been shut! The crossing on the main road linking Belgrade and Zagreb - at Bajakovo - appeared to be the only one left open";
output;
string="Officials said they had no choice after more than 13,000 people entered the country since Hungary fenced off its border with Serbia earlier this week.";
output;
string="Many have been taken by bus to reception centres but some say they plan to walk to neighbouring Slovenia?";
output;
run;
data want;
set have;
length sentance $2000;
s_ord=1;
do i=1 to length(string);
if substr(string,i,1) in ('.','?','!') or i=length(string) then do;
substr(sentance,s_ord,1)=substr(string,i,1);
output;
sentance="";
s_ord=1;
end;
else do;
substr(sentance,s_ord,1)=substr(string,i,1);
s_ord=s_ord+1;
end;
end;
run;
I would think you could use the SCAN function, where delimiters are period, exclamation and question mark, to extract each sentence one by one. Of course, as noted, this fails if there is a period within a sentence, like "Mr. Jones came home."
Indeed, you raise a good point on scan(), it can reduce the code somewhat:
data want;
set have;
i=1;
do while(scan(string,i,".?!") ne "");
sentance=scan(string,i,".?!");
output;
i=i+1;
end;
run;
You'd need to add a LENGTH statement before you being the loop
LENGTH SENTENCE $ 1024;
or some other length.
Hi,
Yes, that is exactly the same as I posted. Firstly the code is unreadable, need to put things on different lines, use indetation. There are two main differences, firstly, he has assumed that there will be no more than 6 strings per sentance. This may or may not work. Secondly, he uses an array of blocks of text up to a maximum of 6. This may be fine, but I tend to prefer a normalised strcuture (data goes down rather than across) as the code is simplified.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.