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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2599 views
  • 1 like
  • 4 in conversation