Editor's Note: This solution provided by Cynthia best addressed the original question.
Hi:
The program below shows the use of the CATT, CATS and CATX functions -- which you can read about in the documentation. In the "fake" data below, variables A1-A5 are character variables; and variables N1-N5 are numeric variables. In order to do the concatenate, you should use the explicit PUT function if you are going to create a character string from numeric values (to avoid conversion errors). There are, of course, many other ways to approach this problem -- but this is an example to get you started with the basic variable value concatenation concepts.
cynthia
data fakedata;
** Vars a1-a5 are character;
** vars n1-n5 are numeric;
length a1 $2 a2 $3 a3 $1 a4 $5 a5 $2;
infile datalines;
input a1 a2 a3 a4 a5 n1 n2 n3 n4 n5;
return;
datalines;
aa xyz q bbbbb tt 11 22 33 44 55
bb xyz r ccccc uu 99 88 77 66 55
;
run;
data cmbcol;
length A N $1000 A_oth N_oth $1000;
set fakedata;
A = catt(a1,a2,a3,a4,a5);
A_oth = catx(':',a1,a2,a3,a4,a5);
N = cats(put(n1, best8.), put(n2, best8.),
put(n3, best8.), put(n4, best8.),put(n5, best8.));
N_oth = catx(':',put(n1, best8.),put(n2, best8.),
put(n3, best8.), put(n4, best8.),put(n5, best8.));
run;
proc print data=cmbcol;
title 'concatenate separate variables into one big variable';
run;
Editor's Note: This solution provided by Cynthia best addressed the original question.
Hi:
The program below shows the use of the CATT, CATS and CATX functions -- which you can read about in the documentation. In the "fake" data below, variables A1-A5 are character variables; and variables N1-N5 are numeric variables. In order to do the concatenate, you should use the explicit PUT function if you are going to create a character string from numeric values (to avoid conversion errors). There are, of course, many other ways to approach this problem -- but this is an example to get you started with the basic variable value concatenation concepts.
cynthia
data fakedata;
** Vars a1-a5 are character;
** vars n1-n5 are numeric;
length a1 $2 a2 $3 a3 $1 a4 $5 a5 $2;
infile datalines;
input a1 a2 a3 a4 a5 n1 n2 n3 n4 n5;
return;
datalines;
aa xyz q bbbbb tt 11 22 33 44 55
bb xyz r ccccc uu 99 88 77 66 55
;
run;
data cmbcol;
length A N $1000 A_oth N_oth $1000;
set fakedata;
A = catt(a1,a2,a3,a4,a5);
A_oth = catx(':',a1,a2,a3,a4,a5);
N = cats(put(n1, best8.), put(n2, best8.),
put(n3, best8.), put(n4, best8.),put(n5, best8.));
N_oth = catx(':',put(n1, best8.),put(n2, best8.),
put(n3, best8.), put(n4, best8.),put(n5, best8.));
run;
proc print data=cmbcol;
title 'concatenate separate variables into one big variable';
run;
Hi Cynthia,
In continuation to earlier communication, I have similar kind of requirement as below but little change..hope you can help on this.
I have consumers name field with character length 80. that i have splited in to multiple columns based on spaces..and now i have got around 15 columns after spliting.
now, my requirement is I need to fit all the 15 columns(that were created above with different lengths) values in to 5 output columns of length 26 each.Can you please suggest and let me know if you need more inputs.
Thanks in advance.
seems to be a very old post when I was searching for a solution for a similar topic. For your question, it might be helpful to use proc transpose
Data X;
set MULTICOL;
ID=_n_;
run;
proc transpose data=X out=want (keep=ID col1 rename=(col1=A) where=(A ne ''));
var A1-A4;
by ID;
run;
Hi,
I need something similar to this, but I would like only UNIQUE Values populated into my new string variable.
What I'm doing is Transposing some information and then srunching all of that items into one variable. I have written a macro to do this, but I can get multiple items sent to my new string variable.
I have added a duplicate record in the datalines. (11 in line 1 and 99 in line 2)
How would I code it so that these duplicated items were only listed once?
data fakedata;
** Vars a1-a5 are character;
** vars n1-n5 are numeric;
length a1 $2 a2 $3 a3 $1 a4 $5 a5 $2;
infile datalines;
input a1 a2 a3 a4 a5 n1 n2 n3 n4 n5;
return;
datalines;
aa xyz q bbbbb tt 11 22 11 44 55
bb xyz r ccccc uu 99 88 99 66 55
;
run;
data cmbcol;
length A N $1000 A_oth N_oth $1000;
set fakedata;
A_oth = catx(',',a1,a2,a3,a4,a5);
N_oth = catx(',',put(n1, best8.),put(n2, best8.),
put(n3, best8.), put(n4, best8.),put(n5, best8.));
run;
proc print data=cmbcol;
title 'concatenate separate variables into one big variable';
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.