Please help me to solve this query:
I have one string as below :
Name = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ;
I need to do these things :-
1. Count the number of words (character strings) separated by blanks in Name1
2. Also I need to get these values too using Name string in a new dataset Name2
Num word
1 |
Is this the real life |
2 |
Is this just fantasy |
3 |
Caught in a landslide |
4 |
No escape from reality |
5 |
|
First, test data step code. Your input means name will have a total of 8 characters maximum depending on the length of the first "word".
Second, you haven't shown what you expect the FIRST output to actually look like.
data junk; Name = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ; run; data want1; set junk; wordcount = countw(name,' '); run; data want2; set junk; do i=1 to countw(name,'.?!'); word = scan(name,i, '.?!'); countwd= countw(word); output; end; drop name; run;
If you need other delimiters such ;:@#$^&* or what have you they can go in the SCAN third position as well.
Just use COUNTW() function.
data have;
input num word $60.;
cards;
1 Is this the real life
2 Is this just fantasy
3 Caught in a landslide
4 No escape from reality
5
;
data want;
set have;
nwords=countw(word,' ');
run;
Results:
Obs num word nwords 1 1 Is this the real life 5 2 2 Is this just fantasy 4 3 3 Caught in a landslide 4 4 4 No escape from reality 4 5 5 0
First, test data step code. Your input means name will have a total of 8 characters maximum depending on the length of the first "word".
Second, you haven't shown what you expect the FIRST output to actually look like.
data junk; Name = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ; run; data want1; set junk; wordcount = countw(name,' '); run; data want2; set junk; do i=1 to countw(name,'.?!'); word = scan(name,i, '.?!'); countwd= countw(word); output; end; drop name; run;
If you need other delimiters such ;:@#$^&* or what have you they can go in the SCAN third position as well.
You seem to have skipped the first step. How to you intend to get from your starting data of one long string into your original posted data of four separate strings?
data example;
paragraph="Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ;
do snum=1 to countw(paragraph,'?.!');
sentence=scan(paragraph,snum,'?.!');
nwords=countw(sentence,' ');
output;
end;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.