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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
 
data w;
k='12 345 main street.';
substr(k,index(k,' '),1)='-';
run;

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20
 
data w;
k='12 345 main street.';
substr(k,index(k,' '),1)='-';
run;
Lost_Gary
Quartz | Level 8

Brilliant.  Thank you.  

art297
Opal | Level 21

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;
  address='12 345 main street.';
  output;
  address='345 main street.';
  output;
run;

data want;
  set have;
  address = prxchange('s/(\d+) (\d+)/$1-$2/', -1, address);
run;

Art, CEO, AnalystFinder.com

 

Lost_Gary
Quartz | Level 8

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.  

art297
Opal | Level 21

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

 

data have;
  address='12 345 main street.';
  output;
  address='345 main street.';
  output;
  address='345 1st avenue';
  output;
run;

data want;
  set have;
  address = prxchange('s/(\d+) (\d+) /$1-$2 /', -1, address);
run;

Art, CEO, AnalystFinder.com

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 5 replies
  • 2725 views
  • 0 likes
  • 3 in conversation