BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Feyng819
Obsidian | Level 7

I would like to remove the substring of the string where the substring starts with number and ends with a word that is contained in the list(ST, STREET,DRIVE,DR). For example: I have “STEPHEN JESSICA 80832 ABC ST” and I want the output to be “STEPHEN JESSICA”. If number is found in the string but it doesn’t end with a word in the list I would like to keep it. I tried to use prxchange but it also detected everything before the numbers.

Here is the example of the data I have.

data have;
Input records $80.;
cards;
STEPHEN JESSICA 234 ABC ST
TOBY 12 BOB
ST JOHN 23 AAA DRIVE
WILLIAM SEAN JAMES 123 XYZ STREET ABBY 456 XXX DRIVE TAMMY LEO OSCAR MARY 789 ZZZ DR ;


And the data I want.

data want;
Input records $80.;
cards;
STEPHEN JESSICA
TOBY 12 BOB
ST JOHN WILLIAM SEAN JAMES ABBY TAMMY LEO OSCAR MARY ;



I tried the following codes to remove the matching string. It fails to remove the number and it also removes the keyword not only at the end of the string but at the beginning as well.

newrecords=prxchange("s\d\STREET|ST|DRIVE|DR$//i",1,records);
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

The regular expression could be:

 

"s/\d.*(STREET|ST|DRIVE|DR)\s*//i"

PG

View solution in original post

4 REPLIES 4
ballardw
Super User

First thing is that the PRXCHANGE code you show uses non-programming "quote" characters. That may be an artifact of pasting into the forum or an actual problem with the source code. If those are unicode quotes SAS can't use them in the editor for programming and should throw an error. Note the curly appearance instead of straight " This is what I got from my SAS session.

385  newrecords=prxchange(“/d\STREET|ST|DRIVE|DR$/“,1,records);
                          -
                          386
                          200
                          76
ERROR 386-185: Expecting an arithmetic expression.

ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR 76-322: Syntax error, statement will be ignored.

386  cards;

Second, if you are getting errors in your log you should include the Log. Copy the entire data step or procedure along with all the messages, notes, warnings and errors created by that step. Then on the forum open a text box using the </> icon above the message window and paste the text.

 

The text box is important to preserve the diagnostic characters that SAS often provides as part of the error message. Note the underscore in the example above under the "quote" character indicating that is what SAS objects to.

Feyng819
Obsidian | Level 7
Thanks for the advice! I have updated the question and the “correct” code I tried, I hope it is more clear now.
PGStats
Opal | Level 21

The regular expression could be:

 

"s/\d.*(STREET|ST|DRIVE|DR)\s*//i"

PG

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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