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

Hi, I have a string that I'm trying to get only a part of.  The format of the original string is like this: '0000 - Stoney Lynn' and I want the function to output 'Stoney Lynn' with no trailing spaces or proceeding spaces.  There is not a standard amount of spaces between the dash: '0000-Stoney Lynn'.  Also sometimes the dash appears to be a longer dash not recognized as '-'.  This is why I think a function looking for characters after any special character may work better and then trim any extra spaces.

 

Heres what I have tried so far: subjectname=trim(compress((substr(subjectid,5)),'-'))

But some of the output comes back with a dash still and the spaces dont trim correctly.

1 ACCEPTED SOLUTION

Accepted Solutions
Kathryn_SAS
SAS Employee

You can use the SCAN function to extract text after a dash. If it is a regular dash, you can use the following:

subjectname=strip(scan(subjectid,2,'-'));

If it is a long dash, you can use the hex representation:

subjectname=strip(scan(subjectid,2,'96'x));

You can look at a variable in hex using a format:

format subjectid $50.;

View solution in original post

1 REPLY 1
Kathryn_SAS
SAS Employee

You can use the SCAN function to extract text after a dash. If it is a regular dash, you can use the following:

subjectname=strip(scan(subjectid,2,'-'));

If it is a long dash, you can use the hex representation:

subjectname=strip(scan(subjectid,2,'96'x));

You can look at a variable in hex using a format:

format subjectid $50.;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1 reply
  • 950 views
  • 2 likes
  • 2 in conversation