Solved
New Contributor
Posts: 4

# Concatenate in do loop with keep blanks doesn't work

I try to concatenate a text-string with a do loop. the functions catt, cats and catx works, but cat doesn't. but i need the blanks in the ouptut string.

```data want;

length string_in string_out_cat string_out_cats string_out_catt string_out_catx \$200 c \$1;

string_in = "this is a textstring";
do i = 1 to lengthc(string_in);
c = substr(string_in,i,1);
string_out_cat = cat(string_out_cat,c);
string_out_cats = cats(string_out_cats,c);
string_out_catt = catt(string_out_catt,c);
string_out_catx = catx("*",string_out_catx,c);
end;

run;

```

any ideas?

Accepted Solutions
Solution
‎06-29-2017 06:02 AM
New Contributor
Posts: 4

## Re: Concatenate in do loop with keep blanks doesn't work

Thanks for your answer. This was the hint I needed. Now it make sence.

I want to keep the space between the word, but I have to loop / process each letter. So the final solution is this:

```data want (drop=c i ablank);

length string_in string_out \$50 c \$1;

string_in = "this is a textstring";
ablank=0;

do i = 1 to lengthc(string_in);
c = substr(string_in,i,1);

if ablank=1 then string_out = cat(strip(string_out)," ",c);
else string_out = cat(strip(string_out),c);

if c=" " then ablank=1;
else ablank=0;

end;

run;

```

All Replies
Super User
Posts: 9,799

## Re: Concatenate in do loop with keep blanks doesn't work

This is due to the way strings are handled.  When you set a new variable of a given length, say 5 in this case then the variable is:

[][][][][]

Now if you choose the procedure that keeps blanks and concatenates variables and add ABCDE:

[][][][][][a][b][c][d][e]

Then put that back into the chracter that has length 5 it chops the end off, hence the variable can only contain 5 spaces.

Thus the question back to you is what do you expect the output to look like?  is it a space between words, or letters?  If its words then you would be better off doing:

```data want;
...
do i=1 to countw(string," ");
word=scan(string,i," ");
end;
run;```

If its each letter, then catx() would be the function.  But without further info I can't say.

Solution
‎06-29-2017 06:02 AM
New Contributor
Posts: 4

## Re: Concatenate in do loop with keep blanks doesn't work

Thanks for your answer. This was the hint I needed. Now it make sence.

I want to keep the space between the word, but I have to loop / process each letter. So the final solution is this:

```data want (drop=c i ablank);

length string_in string_out \$50 c \$1;

string_in = "this is a textstring";
ablank=0;

do i = 1 to lengthc(string_in);
c = substr(string_in,i,1);

if ablank=1 then string_out = cat(strip(string_out)," ",c);
else string_out = cat(strip(string_out),c);

if c=" " then ablank=1;
else ablank=0;

end;

run;

```
☑ This topic is solved.