## Replace a Space with a Dash

Solved
Occasional Contributor
Posts: 9

# Replace a Space with a Dash

This might be easy, but I keep getting hung up on this.  I am trying to replace a space with a dash within an address field.  For instance, the address is listed as 12 345 main street.  I am trying to get it to add a dash, but only in the first space i.e. 12-345 main street.

I appreciate the assistance.

Accepted Solutions
Solution
‎05-21-2018 04:57 PM
PROC Star
Posts: 1,605

## Re: Replace a Space with a Dash

``````
data w;
k='12 345 main street.';
substr(k,index(k,' '),1)='-';
run;``````

All Replies
Solution
‎05-21-2018 04:57 PM
PROC Star
Posts: 1,605

## Re: Replace a Space with a Dash

``````
data w;
k='12 345 main street.';
substr(k,index(k,' '),1)='-';
run;``````
Occasional Contributor
Posts: 9

## Re: Replace a Space with a Dash

Brilliant.  Thank you.

PROC Star
Posts: 8,151

## Re: Replace a Space with a Dash

While you've marked this question as solved, unless all of your addresses have that pattern of numbers, I think you will be exchanging a number of spaces for hyphens where you don't really want to.

I'd suggest using pattern matching. E.g.,

``````data have;
output;
output;
run;

data want;
set have;
run;
``````

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 9

## Re: Replace a Space with a Dash

ooh, I like the efficiency in that coding and also learning different ways.  So a thumbs up for this method as well.

The problem with addresses that I am finding, is the scenario of the address '345 1st avenue' as '345-1st avenue' would be inaccurate (for this scenario).  So, my less than efficient solution was to create different 'rules' to isolate the scenarios in which the 'substr' function works better to replace some of the spaces.  I know what i am doing isn't perfect.

PROC Star
Posts: 8,151

## Re: Replace a Space with a Dash

Precisely why I suggested using pattern matching. That one is easy to correct:

```data have;
output;
output;
output;
run;

data want;
set have;
run;
```

Art, CEO, AnalystFinder.com

☑ This topic is solved.