## Scan function

Solved
Occasional Contributor
Posts: 7

# Scan function

data temp;

set temp1;

length word \$50;

do until (word=" ");

word=scan(summary,count);

output;

end;

run;

I need to split the words of a free text field "Summary". Each word should form one observation. When I run the avove code, it only runs for the first value of the variable "Summary" and then returns returns the remaining as it is. Can anyone please suggest how I could do this for the entire dataset?

This is really urgent. Any help is greatly appreciated.

Thanks!

Accepted Solutions
Solution
‎05-05-2014 12:48 PM
Super User
Posts: 23,771

## Re: Scan function

word=scan(summary,count);

Your counter is i, not count.

word=scan(summary,i);

EDIT:

data temp;

set temp1;

length word \$50;

*set counter;

count=1;

do until (word=" ");

word=scan(summary,count);

output;

*increment counter;

count+1;

end;

run;

All Replies
Super User
Posts: 23,771

## Re: Scan function

Look up the usage of the scan function, you need to add a counter in and remember to increment your counter, which I don't ssee.

Occasional Contributor
Posts: 7

## Re: Scan function

data temp;

set temp1;

length word \$50;

do until (word=" ");

i=1 to n;

word=scan(summary,count);

output;

end;

end;

run;

do you this will work?

Solution
‎05-05-2014 12:48 PM
Super User
Posts: 23,771

## Re: Scan function

word=scan(summary,count);

Your counter is i, not count.

word=scan(summary,i);

EDIT:

data temp;

set temp1;

length word \$50;

*set counter;

count=1;

do until (word=" ");

word=scan(summary,count);

output;

*increment counter;

count+1;

end;

run;

Occasional Contributor
Posts: 7

## Re: Scan function

Thank You very much Reeza.

Regular Contributor
Posts: 217

## Re: Scan function

ranjita,

It appears that your do until is set  to run once.  The default delimiter of scan is the same as your until condition.  Also, once your scan is fixed, your "word" variable will be overwritten each time the do runs.  Please provide examples of values in the variable summary and examples of the results of the word variable if the do and scan are working correctly.

data temp;

set temp1;

length word \$50;

do until (word=" ");

word=scan(summary,count);

output;

end;

run;

Occasional Contributor
Posts: 7

## Re: Scan function

profile_ id       summary               word     count

a1                  I like games            I             1

a1                  I like games           like          2

a1                  I like games          games     3

a1                  I like games                          4

a2                  I watch movies

a3                  I like food

this is the example of the output I am getting. When the profile Id and the the Summary changes, the word does not populate.

Posts: 1,270

## Re: Scan function

Try this.

data want(drop=i);

set have;

i=1;

length word \$50;

do while (scan(summary,i) ne "");

word=scan(summary,i);

output;

i+1;

end;

run;

proc print data=want;

run;

Regular Contributor
Posts: 217

## Re: Scan function

Ranjita,

This worked for me.

data have;
input profile_id \$2. Summary \$50.;
datalines;
a1 I like games
a2 I watch movies
a3 I like food
;
run;

%let words=5; /* assuming the max length of the possible words is 10 */
data want;
format word1 - word&words. \$10.;
set have;
by profile_id;
array val(*) \$ word1 - word&words. ;
if first.profile_id = 1 then do;
do i = 1 to &words;
val(i) = 'empty';
end;
end;
do i=1 to &words.;
val(i) = scan(Summary,i," ");
end;
run;

Super Contributor
Posts: 275

## Re: Scan function

data have;
run;

data want;
set have;
length word \$15;
do count=1 by 1;
word=scan(summary,count);
if (word=" ") then leave;
else output;
end;
drop summary count;
run;

🔒 This topic is solved and locked.