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

Hi,

   As i have difficulties in parsing a string of characters.

   data set like;

   

    orange apple juice and cola

    coffee   candy and paper

    banana sugar and pen and book

 

i just want to parse the characters by 'and',and i have tried the 'scan' function,like ---product = scan(name,i,'and'),using the do loop.

the modifier is 'and' not blank,but the result is not i wanted. i am wondering the better solutions to it.

 

thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

Well, here's another way of doing it, which doesn't require scan at all, relying on the wonderful internal machinery of the infile statement:

data products;
infile cards dsd dlm=' ';
length product $ 30;
input product @@;
if product ne 'and' and not missing(product);
keep product;
cards;
 orange apple juice and cola
    coffee   candy and paper
    banana sugar and pen and book
;
run;

You could stuff around for ages playing with buffer pointers, but if your requirements are as simple as you present, this may be all you need. It also gets around the problematic cANDy!

View solution in original post

4 REPLIES 4
LaurieF
Barite | Level 11

Well, here's another way of doing it, which doesn't require scan at all, relying on the wonderful internal machinery of the infile statement:

data products;
infile cards dsd dlm=' ';
length product $ 30;
input product @@;
if product ne 'and' and not missing(product);
keep product;
cards;
 orange apple juice and cola
    coffee   candy and paper
    banana sugar and pen and book
;
run;

You could stuff around for ages playing with buffer pointers, but if your requirements are as simple as you present, this may be all you need. It also gets around the problematic cANDy!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The reason why scan() does not work in this instance is that it uses single characters as delimeters, i.e. you could look at it like:

scan(name,i,"A" or "N" or "D")

It does not split on words.  Now whilst @LaurieF's strategy may work if you can put the data in datalines, if your data is already exisintg then you can use index or findw to split like:

data want;
  set have;
  length first second $200;
  first=substr(name,1,index(name,"and"));
  second=substr(name,index(name,"and")+3);
run;
JNWong
Calcite | Level 5

thank you!

actually i am sorry for not clarifying my problems. my dataset  have existed in sas. the method you provided works out.

LaurieF's strategy is also a good solution!

LaurieF
Barite | Level 11

In which case, to make sure it always works, in all instances, convert it into a flat file! So many ways to skin a cat:

data all_products;
infile cards;
length all_products $ 80;
input all_products $char80.;
all_products = strip(compbl(all_products));
cards;
 orange apple juice and cola
    coffee   candy and paper
    banana sugar and pen and book
;
run;

filename prodfile temp;

data _null_;
set all_products;
file prodfile;
put all_products;
run;

data products;
infile prodfile dsd dlm=' ';
length product $ 30;
input product @@;
if product ne 'and' and not missing(product);
keep product;
run;

filename prodfile clear;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 802 views
  • 1 like
  • 3 in conversation