Hi,
I would like to concatenate across columns containing a string of single characters to obtain a new column containing the letters arranged in alphabetic order. This will allow me to identify all the people with the same combination.
I would really appreciate any help! Thank you!
data have:
id d1 d2 d3 d4
1 A B C D
2 B D A C
3 D A B C
4 E B F D
etc
data want:
id concat
1 ABCD
2 ABCD
3 ABCD
4 BDEF
etc
Use CALL SORTC to sort the variables then concatename them:
data want;
set have;
array dx d1-d4;
call sortc(of dx(*));
concat=cat(dx(*)); /* OR concat=compres(d1||d2||d3||d4); OR concat=cat(d1,d2,d3,d4) */
drop d1-d4;
run;
CALL SORTC along with CATT
Please review the documentation if you need to review usage.
call sortc(of d1-d4);
x = catt(of d1-d4);
In addition, please consider posting data as a data step, you're close but missing the input statement in your sample.
It helps...since that means I might take a few minutes to actually test this rather than just assume it works and leave the testing to you.
So really, it's in your best interest to post data as a data step.
Use CALL SORTC to sort the variables then concatename them:
data want;
set have;
array dx d1-d4;
call sortc(of dx(*));
concat=cat(dx(*)); /* OR concat=compres(d1||d2||d3||d4); OR concat=cat(d1,d2,d3,d4) */
drop d1-d4;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.