Contributor
Posts: 30

# concatenating but space is removed

Hi all,

I have been trying to join up (concatenate) a series of strings(5) for 100 observations.

some of the strings begin with a space.

EG. " A"

However, when I concatenate it with put:

data A;

set B;

lrecl=900000

put part1 part2 part 3 part4 part5;

run;

So if part1 = "A"   part2= "B"  etc. to part5="E"

one of the parts will have a space as beginning. Such as "AB CDE"

Ideally i want it to be ABCDE, but if there is a space as beginning I need it to be there. such as ABC DE.

So far if I use

put part1 +(-1) part2 +(-)..... part5;

it removes an awkward space that separates the strings but Then it becomes A B C D E, which isnt what I want.

but if I remove the +(-1), it just becomes ABCDE even if there IS a space.

Any ideas? I have thought on using an IF statement. If part1 begins with " ", Then add another space. So then only one space will be removed?

Thank you.

Also I really need to join them up but I'm just stuck in this dilemma on the code fixes one problem but causes the next.

Super User
Posts: 6,939

## Re: concatenating but space is removed

It's a little ugly, but I haven't found anything easier:

array p {5} part1-part5;

do k=1 to 5;

if (' ' < p{k} =: ' ') then put ' ' @ ;

put p{k} +(-1) @ ;

end;

put;

Super User
Posts: 9,866

## Re: concatenating but space is removed

Can you put down a concise correct example of what you have - in the form of a datastep so we can create the data - and what you want out?

I would really question why having a space to start would be necessary - it doesn't really make sense to base any logic on if there is a space as first character, as most functions will trim useless space out.

Normally you would use one of the cat functions e.g:

```want=cat(of part:);
```

But that may not account for preceeding spaces, nor in fact would most (if any at all) of the procedures as it doesn't make logical sense.  I mean is "  A"=" A"??

Contributor
Posts: 30

## Re: concatenating but space is removed

data tags;
set work.complete end=last;
lrecl=90000;
put part1 part2 part3 part4 part5 part6 part7 part8 part9 part10 part11 part12 part13 part14 part15;
run;

RW9, this is the code.

The string is actually an interview so the space in some of the parts are actually needed otherwise it would just mess up the variable names in some cases. Or makes it a weird answer.

This is why...

Thanks!

Super User
Posts: 9,866

## Re: concatenating but space is removed

This doesn't show me the Data. Is is only one space? Is it two, what about spaces after the text? What about missings? Is it only one character after the one space?

Assumes 1 character + 1 optional space:

```data want;
set have;
length want \$2000;
array part{15};
do i=1 to 15;
want=ifc(char(part{i},1)=" ",cat(want,substr(part{i},1,2),cat(want,substr(part{i},1,1));
end;
put want;
run;
```

Can't test this as have no data in the form of a datastep.

Contributor
Posts: 30

## Re: concatenating but space is removed

The characters in bold would be the part 2. So it begins as " total="""

So I need the space.

If I use +(-1) in Put, it takes away the space. and it becomes "total=

Which makes it into one whole word.

But some other cases, the split would be a word, so it might be to tal. which I need to put together without the space...

I hope this is enough information.

Thank you

Discussion stats
• 5 replies
• 129 views
• 0 likes
• 3 in conversation