## making all possible combinations of all words from a given string(N-Grams Concept)

Solved
Frequent Contributor
Posts: 84

# making all possible combinations of all words from a given string(N-Grams Concept)

Hi Experts,

I am trying to get all combinations of words in preceding order available in a given string e.g.

Suppose I have a string  like "The cow jumps over the moon"

and want combinations like..

The cow

The jumps

The over

The the

The Moon

cow jumps

cow over

cow the

cow moon

jumps over

jumps the

jumps moon

over the

over moon

the moon

I m trying the following code:

data test;
sen = "The cow jumps over the moon";
run;

data test1;
set test;
length sen1 \$100.;
sen1="";
retain sen1;
do i=0 to count(compbl(TRIM(sen))," ");
do j=1 to 2;
sen1=compbl(sen1||" "||compbl(trim(scan(compbl(TRIM(sen)),i+j," ","MO"))));
ngram= j;
r=count(TRIM(sen)," ");
x=count(compbl(TRIM(sen))," ");
output;
end;
sen1="";
end;
if ngram=2;
run;

Thnx

Rahul

Accepted Solutions
Solution
‎01-03-2017 08:57 AM
Super User
Posts: 6,934

## Re: making all possible combinations of all words from a given string(N-Grams Concept)

Using the data you already have:

data want;

set test;

nitems = countw(sen);

length combo \$ 100;

if nitems > 1;

do i=1 to nitems - 1;

do j=i+1 to nitems;

combo = catx(' ', scan(sen, i), scan(sen, j));

output;

end;

run;

All Replies
Super Contributor
Posts: 355

## Re: making all possible combinations of all words from a given string(N-Grams Concept)

[ Edited ]
``````Data A;
Input Text : \$ Nr @@;
Datalines;
The 1 cow 2 jumps 3 over 4 the 5 moon 6
;
***2nd select did not really make sense ..*;
Proc SQL;
Create Table B As
Select a1.Text As Text1,a2.Text As Text2 From A a1,A a2
Where a1.Nr lt a2.Nr ;
Quit;``````
Solution
‎01-03-2017 08:57 AM
Super User
Posts: 6,934

## Re: making all possible combinations of all words from a given string(N-Grams Concept)

Using the data you already have:

data want;

set test;

nitems = countw(sen);

length combo \$ 100;

if nitems > 1;

do i=1 to nitems - 1;

do j=i+1 to nitems;

combo = catx(' ', scan(sen, i), scan(sen, j));

output;

end;

run;

New User
Posts: 1

## Re: making all possible combinations of all words from a given string(N-Grams Concept)

Hey dude I need your number. Your coding concept with SAS is really close to something that is pretty cool and I am willing to give you a % of the money my fully developed code is worth.

Max

4045380741

Super User
Posts: 6,934

## Re: making all possible combinations of all words from a given string(N-Grams Concept)

These message boards make it easy to send private messages.  But you have to enable that feature for your userid.

Assuming you were talking to me, I know that I can't take on any new projects until the end of the summer.

☑ This topic is solved.