BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

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

marysasfan_0-1671731058575.png

https://www.extendoffice.com/documents/excel/1783-excel-remove-text-before-character.html

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@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,'?');
--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

What SAS interface are you talking about? Base SAS? SAS Enterprise Guide? Something else?

--
Paige Miller
Emma_at_SAS
Lapis Lazuli | Level 10

I use SAS 9.4 for windows. Does this answer your question?

 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Emma_at_SAS
Lapis Lazuli | Level 10

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

PaigeMiller
Diamond | Level 26

@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,'?');
--
Paige Miller
Tom
Super User Tom
Super User

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.

Emma_at_SAS
Lapis Lazuli | Level 10

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!

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 4054 views
  • 2 likes
  • 3 in conversation