- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The regular expression could be:
"s/\d.*(STREET|ST|DRIVE|DR)\s*//i"
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The regular expression could be:
"s/\d.*(STREET|ST|DRIVE|DR)\s*//i"
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content