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!
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!
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!
Thank you! I was using a combination of substr and index and wasn't quite getting what I wanted. Thanks again.
Another way is to COMPRESS out all the characters you don't want.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.