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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 580 views
  • 0 likes
  • 3 in conversation