Hello all,
Thanks to Kit Baum for his help in adding strkeep to SSC.
strkeep is a small command used for data management and cleaning string variables and varlists by keeping only whitelisted characters.
I created this command after spending too much time using subinstr() to find and replace individual special characters in variables containing first and last names. Instead of finding characters that you want to remove, strkeep finds characters that you want to keep and removes all other characters.
For example, instead of doing the following to try to clean two variables so that they only have letters in them...
You could do...
In essence, strkeep is a "whitelist" command, while using a function like subinstr() to clean variables is a "blacklist" command.
Please let me know if you have any questions or suggestions. I hope this command proves useful to people other than myself!
Best,
Roger
Thanks to Kit Baum for his help in adding strkeep to SSC.
strkeep is a small command used for data management and cleaning string variables and varlists by keeping only whitelisted characters.
I created this command after spending too much time using subinstr() to find and replace individual special characters in variables containing first and last names. Instead of finding characters that you want to remove, strkeep finds characters that you want to keep and removes all other characters.
For example, instead of doing the following to try to clean two variables so that they only have letters in them...
Code:
replace firstname = subinstr(firstname,".","",.) replace firstname = subinstr(firstname,"'","",.) replace firstname = subinstr(firstname," ","",.) replace firstname = subinstr(firstname,"`","",.) replace firstname = subinstr(firstname,"-","",.) replace lastname = subinstr(lastname,".","",.) replace lastname = subinstr(lastname,"'","",.) replace lastname = subinstr(lastname," ","",.) replace lastname = subinstr(lastname,"`","",.) replace lastname = subinstr(lastname,"-","",.)
Code:
strkeep firstname lastname, replace alpha
Please let me know if you have any questions or suggestions. I hope this command proves useful to people other than myself!
Best,
Roger
Comment