SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kmardinian
Quartz | Level 8

Hi, I have a variable called "comment" that contains a string of words that are separated by '~'

 

example:

comment

text text text text ~ text text

text text text text text ~ text text text ~text text 

 

I would like to extract the text after the first ~ without losing the text behind the second or third or fourth ~ etc.

 

I tried using this code:

cm=scan(comment,1,"~");

 

But it only extracts the text after the first ~ and then stops once it reaches the second ~

 

Any help is much appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Use INDEX() to find the first tilda and then use that number in SUBSTR().
Double check the order of t

cm = substr(comment, index(comment, '~') +1);

View solution in original post

3 REPLIES 3
Reeza
Super User
Use INDEX() to find the first tilda and then use that number in SUBSTR().
Double check the order of t

cm = substr(comment, index(comment, '~') +1);
kmardinian
Quartz | Level 8

Hi Reeza,

 

Thank you for the quick response. Out of curiosity, what does the +1 signify?

 

 

Reeza
Super User
INDEX would find the ~, so if you substring from that it includes the ~, +1 gives you the position after the first ~.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 3 replies
  • 81341 views
  • 2 likes
  • 2 in conversation