BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sdixit
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
sdixit
Obsidian | Level 7

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

11 REPLIES 11
PGStats
Opal | Level 21

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
Astounding
PROC Star

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.

sdixit
Obsidian | Level 7

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

Astounding
PROC Star

What variable (or variables) exist in your data? 

 

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

sdixit
Obsidian | Level 7

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.

Astounding
PROC Star

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+

sdixit
Obsidian | Level 7

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.

 

 

Astounding
PROC Star

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?

ballardw
Super User

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.

sdixit
Obsidian | Level 7

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

sdixit
Obsidian | Level 7

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.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 11 replies
  • 1852 views
  • 0 likes
  • 4 in conversation