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

Say we have 4 letters--"A", "B","C","D". Now I want to get the combinations, the result should be 16 ELEMENTS.

AA
AB
AC
AD
BA
BB
BC
BD
CA
CB
CC
CD
DA
DB
DC
DD

Thank you.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Not sure what you're asking for. Is it something like:

 

data want (keep=first: second:);
  array first(16) $;
  array second(16) $;
  do i = 'A', 'B', 'C', 'D';
    do j = 'A', 'B', 'C', 'D';
      cell+1;
      first(cell)=i;
      second(cell)=j ;
    end;
  end;
run;  

Art, CEO, AnalystFinder.com

 

View solution in original post

5 REPLIES 5
collinelliot
Barite | Level 11
data want;
do i = 'A', 'B', 'C', 'D';
do j = 'A', 'B', 'C', 'D';
var = cats(i, j);
output;
end;
end;
run;
sas_newbie3
Obsidian | Level 7

Hi,

I want the result can be an arrary format, so I can retrieve the first element and second item etc.

So for example, for AA, it is an array. The first item of the array is A, the second item of array is A also.

collinelliot
Barite | Level 11

I'm really sorry, but I don't understand. The code I provided gives you the results you specified. The i and j give you the two separate portions, if that's what you mean. 

art297
Opal | Level 21

Not sure what you're asking for. Is it something like:

 

data want (keep=first: second:);
  array first(16) $;
  array second(16) $;
  do i = 'A', 'B', 'C', 'D';
    do j = 'A', 'B', 'C', 'D';
      cell+1;
      first(cell)=i;
      second(cell)=j ;
    end;
  end;
run;  

Art, CEO, AnalystFinder.com

 

ballardw
Super User

@sas_newbie3 wrote:

Hi,

I want the result can be an arrary format, so I can retrieve the first element and second item etc.

So for example, for AA, it is an array. The first item of the array is A, the second item of array is A also.


Then go back to your orginal post and change the "example" data to what you actually want. Your example implied one record per two character concatenated string.

 

And your description would result in 32 values not 16:

A,A,A,B,A,C,A,D,B,A,B,B,B,C,B,D,C,A,C,B,C,C,C,D,D,A,D,B,D,C,D,D

 

data junk;
   array x {32} $1.;
   counter=0;
   do i= 'A','B','C','D';
      do j= 'A','B','C','D';
         counter+1;
         x[counter]=i ;
         counter+1;
         x[counter]=j ;
      end;
   end;
   output;
   drop counter i j;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 826 views
  • 5 likes
  • 4 in conversation