BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

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

Aexor
Lapis Lazuli | Level 10
I need to use This string and get values of Num and Word

Name = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ;
ballardw
Super User

@Aexor wrote:
I need to use This string and get values of Num and Word

Name = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ;

How does @Tom 's code not work? Explicit descriptions are needed.

Aexor
Lapis Lazuli | Level 10
This is not matching with my requirement .
In my requirement I have only a string Name which is having value as "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ;
data have ;
Input Name $ ;
Name = "Is this the real life? Is this just fantasy? Caught in a landslide. No escape from reality!" ;
run;
1. The first requirement is Count the number of words (character strings) separated by blanks in new variable Name1

2. The second requirement is
I have to create a dataset (NAME2 ) using dataset have where I can separate out each string delimited with "? or . or ! " and also add sequence number so my output will look like this

data Want
NUM Word
1 Is this the real life
2 Is this just fantasy
3 Caught in a landslide
4 No escape from reality
5


I hope I am making some sense here
ballardw
Super User

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.

 

Tom
Super User Tom
Super User

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;

image.png

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
  • 6 replies
  • 1877 views
  • 4 likes
  • 3 in conversation