DATA Step, Macro, Functions and more

concatenate strings across columns

Accepted Solution Solved
Reply
Contributor m_o
Contributor
Posts: 30
Accepted Solution

concatenate strings across columns

[ Edited ]

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


Accepted Solutions
Solution
‎05-01-2017 11:43 PM
Trusted Advisor
Posts: 1,558

Re: concatenate strings across columns

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;

View solution in original post


All Replies
Super User
Posts: 19,789

Re: concatenate strings across columns

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. 

 

Solution
‎05-01-2017 11:43 PM
Trusted Advisor
Posts: 1,558

Re: concatenate strings across columns

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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 169 views
  • 2 likes
  • 3 in conversation