BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
J_Yetman
Fluorite | Level 6

There's a business need to create a list of all possible 5-character length combinations of 24 capital letters and 8 numbers (leaving out I, O, 1 and 0).

 

I have some basic code to create the list, using an array.  However, when outputting to the table, I'm only seeing the output of the first character of the string:

 

I'm looking for:

 

X                          count

AAAAA              1

AAAAB              2

AAAAC              3

....

99997                 33554430

99998                 33554431

99999                 33554432

 

And what I'm getting is 

X                          count

A                         1

A                         2

A                         3

....

9                          33554430

9                          33554431

9                          33554432

 

This is my code:

data x;
length x $5.;
ct=0;
Array a{32} $32 ('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'2','3','4','5','6','7','8','9');
do c1=1 to 32;
do c2=1 to 32;
do c3=1 to 32;
do c4=1 to 32;
do c5=1 to 32;
x=cat(a[c1],a[c2],a[c3],a[c4],a[c5]);
ct=ct+1;
output;
end;
end;
end;
end;
end;
drop a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 c1 c2 c3 c4 c5;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @J_Yetman,

 

Inserting the "s" in x=cats(a[c1], ...) will solve the issue. (The code could also be simplified, but the time to do that might easily exceed the run time of only a few seconds.)

View solution in original post

6 REPLIES 6
FreelanceReinh
Jade | Level 19

Hello @J_Yetman,

 

Inserting the "s" in x=cats(a[c1], ...) will solve the issue. (The code could also be simplified, but the time to do that might easily exceed the run time of only a few seconds.)

ballardw
Super User

Your code does PERMUTATIONS, where order matters not combinations. In Combinations AAAAB and BAAAA are the same combination of values.

 

You can also fix your code by using this array definition:

Array a{32} $1 ('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'2','3','4','5','6','7','8','9');

Your definition created 32 variables each of length 32. Which meant that the CAT function was using lots of trailing blanks.

 

J_Yetman
Fluorite | Level 6
Thanks - I should've clarified, for this business case, they treat 'AAAAB' and 'BAAAA' as a different combination.
ballardw
Super User

@J_Yetman wrote:
Thanks - I should've clarified, for this business case, they treat 'AAAAB' and 'BAAAA' as a different combination.

Repeat correction: "different permuation". By definition it is the same combination in mathematical and generally programming terminology.

 

I spent one semester dealing with pretty  much ways to count combinations and permutations of values and had that difference driven in pretty hard.

Tom
Super User Tom
Super User

Looks like you got the right number.  But you did a lot of extra work to find it.

621  data _null_;
622    np = 32**5 ;
623    put np= comma32.;
624  run;

np=33,554,432

Are you making license plates?

734  data plates;
735    letters='ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
736    length plate $5 ;
737    do ct=0 to length(letters)**5-1 ;
738      temp=ct;
739      do pos=5 to 1 by -1 ;
740        substr(plate,pos,1) = char(letters,1+mod(temp,32));
741        temp=int(temp/32);
742      end;
743      output;
744    end;
745    drop temp letters pos;
746  run;

NOTE: The data set WORK.PLATES has 33554432 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           4.80 seconds
      cpu time            4.64 seconds


747
748  data _null_;
749    set plates nobs=nobs ;
750    if _n_ in (1:5) or _n_ > nobs-5 ;
751    put plate ct :comma32.;
752  run;

AAAAA 0
AAAAB 1
AAAAC 2
AAAAD 3
AAAAE 4
99995 33,554,427
99996 33,554,428
99997 33,554,429
99998 33,554,430
99999 33,554,431
NOTE: There were 33554432 observations read from the data set WORK.PLATES.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 674 views
  • 2 likes
  • 4 in conversation