For a variable, I want to remove all text after a question mark (?) and for another variable, I want to remove all text before a space ( ). I know how to do it in Excel like the following note for a comma, for example. But I do not know that in SAS. Thanks for your help
https://www.extendoffice.com/documents/excel/1783-excel-remove-text-before-character.html
@Emma_at_SAS wrote:
I am looking for a line of code or SAS syntax that can do a similar data cleaning for me in SAS.
To remove text before a blank space
want1 = substr(text_string1, find(text_string1,' ')+1);
Remove text after a question mark
want2 = scan(tex_string2,1,'?');
What SAS interface are you talking about? Base SAS? SAS Enterprise Guide? Something else?
I use SAS 9.4 for windows. Does this answer your question?
Yes, that's it. But now I'm confused, I don't think the Find and Replace dialog box in SAS 9.4 matches what is shown at that link, so I still don't know how to answer the question.
I am looking for a line of code or SAS syntax that can do a similar data cleaning for me in SAS. Currently, I have to send my data to Excel, do this step, and read it again in SAS. I want to be able to do this in SAS. Thanks
@Emma_at_SAS wrote:
I am looking for a line of code or SAS syntax that can do a similar data cleaning for me in SAS.
To remove text before a blank space
want1 = substr(text_string1, find(text_string1,' ')+1);
Remove text after a question mark
want2 = scan(tex_string2,1,'?');
Are you asking about how to edit your program? If so then what is the use case where it makes any sense to try to automatic modifying code in that way?
If not then why are you talking about a GUI tool like the find/replace tool of Excel?
If you are talking about modifying data then just use CODE to make the changes.
SCAN() is useful for finding text up to particular character.
beginning=scan(string,1,',');
Note that if the string does not have any commas then you get the whole string.
For finding text after a character you probably want to use FIND() or INDEX() to locate the first occurence of the character and then use SUBSTR().
ending=substr(string,index(string,',')+1);
Again if there are no commas then it will return the whole string.
Thank you @PaigeMiller and @Tom for taking the time to check my question. Your suggestions for using substr and scan does exactly what I was looking for. Thanks!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.