Hello,
I need to concatenate 100 columns of numeric values. An small example dataset is provided below. I want the concatenated variable to be separated by a space, so the variable reads like this: "41 42 43 44 45 46 47"; "61 62 63 64". Note that some rows have missing values (they don't have a value for each column).
data have;
input col1 col2 col3 col4 col5 col6 col7;
cards;
41 42 43 44 45 46 47
51 52 53 54 55 57 .
61 62 63 64 . . .
71 . . . . . .
81 82 83 84 . . .
;
I tried the following code, as well as some other modifications, but can't quite get the correct answer. The following code does not include a space and includes "." for columns with missing values ("61626364...").
data want; set have;
all=catt(' ', of col:);
drop col:;
run;
Any advice?
HI @luch25 Would this help?
data have;
input col1 col2 col3 col4 col5 col6 col7;
cards;
41 42 43 44 45 46 47
51 52 53 54 55 57 .
61 62 63 64 . . .
71 . . . . . .
81 82 83 84 . . .
;
data want;
set have;
want=translate(catx(' ',of col:),' ','.');
run;
HI @luch25 Would this help?
data have;
input col1 col2 col3 col4 col5 col6 col7;
cards;
41 42 43 44 45 46 47
51 52 53 54 55 57 .
61 62 63 64 . . .
71 . . . . . .
81 82 83 84 . . .
;
data want;
set have;
want=translate(catx(' ',of col:),' ','.');
run;
Minor variation of the solution posted by @novinosrin :
options missing=' ';
data want;
set have;
length want $ 100;
want = catx(' ', of col:);
run;
options missing='.';
Without the length-statement catx sets the length to 200 chars, if you list is far longer than those seven variables you should, truncation should be expected.
Alternative way is :
data have; input col1 col2 col3 col4 col5 col6 col7; cards; 41 42 43 44 45 46 47 51 52 53 54 55 57 . 61 62 63 64 . . . 71 . . . . . . 81 82 83 84 . . . ; options missing=' '; data want; set have; want=catx(' ',of col:); 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.