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

Hello SAS Community,

 

I am attempting to concatenate two variables, however, I would like to always retain the order of the two variables that I am concatenating because I would like to also create a table that summarizes the concatenated variables (sometimes the order is reversed [i.e. pair1,pair2 is also pair2,pair1]), but I would like to count it as 1 (irrespective of the order of the concatenated variable). Please see the sample data and what I am trying to accomplish below. I hope this makes sense. Any help is greatly appreciated!

 

 

Picture1.png

 

SAS CODE TO CONCATENATE:

 

data data1;

set mydata;

cat_pair=catx(',',pair_a,pair_b);

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data data1;
set mydata;
call sortc(pair_a, pair_b);
cat_pair=catx(',',pair_a,pair_b);
run;

proc freq data=data1;
table cat_pair / out = want;;
run;
 
proc print data=want;run;

Sort your variables ahead of time. 


@cbourne090 wrote:

Hello SAS Community,

 

I am attempting to concatenate two variables, however, I would like to always retain the order of the two variables that I am concatenating because I would like to also create a table that summarizes the concatenated variables (sometimes the order is reversed [i.e. pair1,pair2 is also pair2,pair1]), but I would like to count it as 1 (irrespective of the order of the concatenated variable). Please see the sample data and what I am trying to accomplish below. I hope this makes sense. Any help is greatly appreciated!

 

 

Picture1.png

 

SAS CODE TO CONCATENATE:

 

data data1;

set mydata;

cat_pair=catx(',',pair_a,pair_b);

run;

 


 

View solution in original post

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

Like this?

proc sql;
   select catx(',', A, B) as PAIR, count(*) as COUNT from HAVE group by 1;

 

Reeza
Super User
data data1;
set mydata;
call sortc(pair_a, pair_b);
cat_pair=catx(',',pair_a,pair_b);
run;

proc freq data=data1;
table cat_pair / out = want;;
run;
 
proc print data=want;run;

Sort your variables ahead of time. 


@cbourne090 wrote:

Hello SAS Community,

 

I am attempting to concatenate two variables, however, I would like to always retain the order of the two variables that I am concatenating because I would like to also create a table that summarizes the concatenated variables (sometimes the order is reversed [i.e. pair1,pair2 is also pair2,pair1]), but I would like to count it as 1 (irrespective of the order of the concatenated variable). Please see the sample data and what I am trying to accomplish below. I hope this makes sense. Any help is greatly appreciated!

 

 

Picture1.png

 

SAS CODE TO CONCATENATE:

 

data data1;

set mydata;

cat_pair=catx(',',pair_a,pair_b);

run;

 


 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1033 views
  • 0 likes
  • 3 in conversation