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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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;
luch25
Obsidian | Level 7
Yes, thank you!!
andreas_lds
Jade | Level 19

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.

Ksharp
Super User

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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

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
  • 4 replies
  • 1678 views
  • 1 like
  • 4 in conversation