SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 1200 views
  • 0 likes
  • 3 in conversation