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

Hello together,

I want to concatenate the values of the columne tax_nr (alphanumeric with 12 character length) on my unique primary key. For example:

Table Customer:

Cust_Nr                 tax_Nr

123456789            12345B

123456789            12345C

123456789            12345D

553456789             22235B

553456789             22235K

553456777             33239Z

The result shall seem after selection as follows:

Cust_Nr                  number_of_pk              tax_Nr

123456789             3                                   12345B, 12345C, 12345D

553456789              2                                   22235B, 22235K

553456777              1                                   33239Z

I tried it by following syntax:

select Cust_Nr,

           count(Cust_Nr)      AS number_of_pk,

          case when count( Cust_Nr) > 1 then put(input(catx(",",tax_Nr),best12.),best12.)

          else tax_Nr

          end      AS  tax_Nr_NEW

From t1.customer

group by Cust_Nr;

However it doesn't work. Any clues about what shall I change in syntax? Thank you in advance.

Best regards

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

SQL is not best suited to the task you describe.  It is built to deal with normalised data, not transposed, which is what you want as output.  I would suggest:

data want;

     set have;

     length tax_nr_string $200;

     retain tax_nr_string number_of_pk;

     by cust_nr;

     if first.cust_nr then do;

          tax_nr_string=tax_nr;

          number_of_pk=1;

     end;

     else do;

          tax_nr_string=catx(',',tax_nr_string,tax_nr);  /* If you get blanks then put a strip() function around each */

          number_of_pk=number_of_pk+1;

     end;

     if last.cust_nr then output;

run;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

SQL is not best suited to the task you describe.  It is built to deal with normalised data, not transposed, which is what you want as output.  I would suggest:

data want;

     set have;

     length tax_nr_string $200;

     retain tax_nr_string number_of_pk;

     by cust_nr;

     if first.cust_nr then do;

          tax_nr_string=tax_nr;

          number_of_pk=1;

     end;

     else do;

          tax_nr_string=catx(',',tax_nr_string,tax_nr);  /* If you get blanks then put a strip() function around each */

          number_of_pk=number_of_pk+1;

     end;

     if last.cust_nr then output;

run;

Kurt_Bremser
Super User

catx(",",tax_Nr) will yield one of your tax_Nr, which is not entirely numeric, therefore the input with best12. will fail.

My solution for such tasks is a data step:

proc sort data=have;

by Cust_Nr;

run;

data want (keep=Cust_Nr number_of_pk tax_Nr_NEW);

set have;

by Cust_Nr;

length tax_nr_NEW $ 50; *size this sufficiently big to hold all possible tax_Nr;

retain

  tax_Nr_NEW

  number_of_pk

;

if first.Cust_Nr then do;

  tax_Nr_NEW = '';

  number_of_pk = 0;

end;

tax_Nr_NEW = catx(',',trim(tax_Nr_NEW,trim(tax_nr));

number_of_pk + 1;

if last.Cust_Nr then output;

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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