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!
SAS CODE TO CONCATENATE:
data data1;
set mydata;
cat_pair=catx(',',pair_a,pair_b);
run;
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!
SAS CODE TO CONCATENATE:
data data1;
set mydata;
cat_pair=catx(',',pair_a,pair_b);
run;
Like this?
proc sql;
select catx(',', A, B) as PAIR, count(*) as COUNT from HAVE group by 1;
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!
SAS CODE TO CONCATENATE:
data data1;
set mydata;
cat_pair=catx(',',pair_a,pair_b);
run;
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.
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.
Ready to level-up your skills? Choose your own adventure.