BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Krysia24
Obsidian | Level 7

I have data that look like this: 

 

Chocolate chips (2)

Brown sugar (1)

Eggs 3 

Flour (1) 

Butter 2 

 

And what I want is just: 

Chocolate chips 

Brown sugar 

Eggs

Flour 

Butter 

 

Basically I want to retain all text prior to a digit or special character without compressing. 

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

SCAN lets you specify your delimiters, try using all the digits and the opening parenthesis to see if that gives you what you need.
You can also look at the modifiers for slightly simpler code, but this should works.

data have;
input string_variable : & $40.;
cards;
Chocolate chips (2)
Brown sugar (1)
Eggs 3 
Flour (1) 
Butter 2 
;;;;
run;

data want;
set have;

want = scan(string_variable, 1, "01234567890(");
run;

@Krysia24 wrote:

I have data that look like this: 

 

Chocolate chips (2)

Brown sugar (1)

Eggs 3 

Flour (1) 

Butter 2 

 

And what I want is just: 

Chocolate chips 

Brown sugar 

Eggs

Flour 

Butter 

 

Basically I want to retain all text prior to a digit or special character without compressing. 

 

Thanks!

 





View solution in original post

3 REPLIES 3
Reeza
Super User

SCAN lets you specify your delimiters, try using all the digits and the opening parenthesis to see if that gives you what you need.
You can also look at the modifiers for slightly simpler code, but this should works.

data have;
input string_variable : & $40.;
cards;
Chocolate chips (2)
Brown sugar (1)
Eggs 3 
Flour (1) 
Butter 2 
;;;;
run;

data want;
set have;

want = scan(string_variable, 1, "01234567890(");
run;

@Krysia24 wrote:

I have data that look like this: 

 

Chocolate chips (2)

Brown sugar (1)

Eggs 3 

Flour (1) 

Butter 2 

 

And what I want is just: 

Chocolate chips 

Brown sugar 

Eggs

Flour 

Butter 

 

Basically I want to retain all text prior to a digit or special character without compressing. 

 

Thanks!

 





Krysia24
Obsidian | Level 7

Thank you! I was using a combination of substr and index and wasn't quite getting what I wanted. Thanks again. 

ChrisNZ
Tourmaline | Level 20

Another way is to COMPRESS out all the characters you don't want.

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
  • 3 replies
  • 1257 views
  • 0 likes
  • 3 in conversation