DATA Step, Macro, Functions and more

Combinations and Permutations

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Combinations and Permutations

Hi All,

 

I have a numeric value or string like

1-4

5

5+

5-

6

6+

6-

7

7+

7-

8

9

9+

9-

10

 

And I would like to create a string concatinated from above using combination and permutition of any combination of above numbers.

 

 

and then I need to check in my data if the variable has any of the combination of the string defined above.

Possible Unique Combinations

#  in String

Example

15

1

1-4

105

2

1-4, 5

455

3

1-4, 5, 5+

1365

4

1-4,5, 5+, 5-

3003

5

1-4, 5, 5+, 5-, 6

5005

6

1-4, 5, 5+, 5-, 6, 6+

6435

7

1-4, 5, 5+, 5-, 6, 6+ 6-

6435

8

1-4, 5, 5+, 5-, 6, 6+, 6-, 7

5005

9

1-4, 5, 5+, 5-, 6, 6+, 6-, 7, 7+

3003

10

1-4, 5, 5+, 5-, 6, 6+, 6-, 7, 7+, 7-

1365

11

1-4, 5, 5+, 5-, 6, 6+, 6-, 7, 7+, 7-, 8

455

12

1-4, 5, 5+, 5-, 6, 6+, 6-, 7, 7+, 7-, 8, 9

105

13

1-4, 5, 5+, 5-, 6, 6+, 6-, 7, 7+, 7-, 8, 9, 9+

15

14

1-4, 5, 5+, 5-, 6, 6+, 6-, 7, 7+, 7-, 8, 9, 9+, 9-

1

15

1-4, 5, 5+, 5-, 6, 6+, 6-, 7, 7+, 7-, 8, 9, 9+, 9-, 10

 

Can anyone help?


Accepted Solutions
Solution
‎07-20-2016 01:18 PM
Contributor
Posts: 26

Re: Combinations and Permutations

data want;   

 

 set have;      

  array lookup(5)  $10 _temporary_ ('8' '9+' '9' '9-' '10');      

  n=countw(abc);      

  flag='N';      

   do i=1 to n;          

     part=strip(scan(abc, i, ","));       

         if whichc(part, of lookup(*))>0 then                       flag='Y';     

   end;               

keep abc flag ;run;

 

This works. Thanks for you help.

View solution in original post


All Replies
Respected Advisor
Posts: 4,919

Re: Combinations and Permutations

Since every combination is allowed, wouldn't it be more efficient to check that all elements of the variable are among the allowed 15 substrings?

PG
Super User
Posts: 5,498

Re: Combinations and Permutations

Check your data?  What data?  What are you trying to check?

 

It's not that difficult to create 32K combinations (at least I assume you want combinations, but not permutations ... combinations would match with the column in your chart about Possible Unique Combinations).  But what do you want to do once you have them?  That part is very far from being clear.

Contributor
Posts: 26

Re: Combinations and Permutations

Posted in reply to Astounding

I want to check in if my data matches with any combination of the 15 strings

Super User
Posts: 5,498

Re: Combinations and Permutations

What variable (or variables) exist in your data? 

 

Give us 3 examples of a match and 3 examples of a mismatch.

Contributor
Posts: 26

Re: Combinations and Permutations

Posted in reply to Astounding

Sure,

 

Lets Take in Example :

 

consider "1-4, 5, 5+, 5-, 6, 6+ 6-" as a string created from the combination of any number from the string mentioned above.

 

 

And My data also has the same string ""1-4, 5, 5+, 5-, 6, 6+ 6-"  for a record

and other raw has  "8,9 ,10 " which does not matches from the above string.

Super User
Posts: 5,498

Re: Combinations and Permutations

So your data set contains a long string as a single variable.  If your data contains this, it is a match:

 

1-4, 5, 5+, 5-, 6, 6+, 6-

 

What if your data contains this instead, is it still a match:

 

6-, 1-4, 5, 5+, 5-, 6, 6+

Contributor
Posts: 26

Re: Combinations and Permutations

Posted in reply to Astounding

yes , This should be a match, Thats why I asked to have a logic which will create any conbination and any permutation of the string.

 

 

Super User
Posts: 5,498

Re: Combinations and Permutations

Then I agree with PGStats.  You don't need to create all combinations and permutations.  You just need to check each element of your data against a list of 15 possibilities.  There are many ways to go about this.  No matter which way you choose, you need to be able to move through your data elements:

 

match='Y';

length nextword $ 3;

if long_string > ' ' then do _n_=1 to countw(long_string, ',');

   nextword = scan(long_string, _n_, ',');

   * some sort of checking here that might set MATCH to "N" ;

end;

 

Do you need more of the details on how to check?

Super User
Posts: 11,343

Re: Combinations and Permutations

[ Edited ]
Posted in reply to Astounding

Expanding on @Astounding and @PGStats :

 

data want;
   set have;
   array test (15) $ 3 _temporary_('1-4','5','5+','5-','6','','6+','6-','7','7+','7-','8','9','9+','9-','10');
   length nextword $ 3;
   Match='Yes';
   if not missing(long_string) then do _n_=1 to countw(long_string,', ');
      nextword = scan(long_string,_n_,', '); 
      if whichc(nextword,of test(*)) = 0 then Match='NO';
   end;
   drop nextword;
run;
 

Could add a If Match='NO' then Leave; to shorten the runtime but likely to be insignificant unless you have a large input set.

 

Long_string is the name of your string variable you are testing.

Contributor
Posts: 26

Re: Combinations and Permutations

[ Edited ]

Thanks

The requirement has changed a bit.

 

so now my motive is to create a flag when my variable matches with any number from ('8', '9+',' 9', '9-', '10')  string.

So if there is any number present in my variable ABC , it should set the flag = "Y".

 

My data looks like :

 

ABCFlag
5,7,8Y
9,7,4Y
8Y
9Y
9+Y
9-Y
10Y
5,6N
3,2N

 

 

You suggestion works if we have 2 number or greater in the string.but if there is only 1 digit then it wont work. Pls suggest for that

Solution
‎07-20-2016 01:18 PM
Contributor
Posts: 26

Re: Combinations and Permutations

data want;   

 

 set have;      

  array lookup(5)  $10 _temporary_ ('8' '9+' '9' '9-' '10');      

  n=countw(abc);      

  flag='N';      

   do i=1 to n;          

     part=strip(scan(abc, i, ","));       

         if whichc(part, of lookup(*))>0 then                       flag='Y';     

   end;               

keep abc flag ;run;

 

This works. Thanks for you help.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 478 views
  • 0 likes
  • 4 in conversation