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

Hi,

can you please provise me solution of follwing problem.

 

I want to count no of words from following data with the help of do loop only (do not use COUNTW function ).

 

 data a;
input name $ 1-30;
datalines;
i am vinayak more
asif shaikh is my friend
;
run;

 

output should be like this:

 


          name                        count
i am vinayak more                4
asif shaikh is my friend         5

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RahulG
Barite | Level 11

The below code would also take care if there are multiple blanks

 

data a;
input name $ 1-30;
datalines;
i   am vinayak more
asif shaikh   is my friend
;
run;

 

 

data new(drop = len c char_val last_char_val);
length last_char_val $1. ;
retain last_char_val;
set a;
len=length(name);
c=1;word_count=0;
do while(c<=len);
char_val=substr(name,c,1);
if char_val eq ' ' and last_char_val ne ' ' then word_count +1;
c+1;
last_char_val=char_val;
end;
word_count +1;
run;

View solution in original post

6 REPLIES 6
RahulG
Barite | Level 11

There can be many ways...below mentioned is counting of blanks by compressing blank.

 

 

data new;
set a;
word_count=length(name) - length(compress(name)) +1 ;
run;

_sas
Calcite | Level 5
Thanks rahul.
can you plz tell me how can i find this count with the help of do loop ?
RahulG
Barite | Level 11

The below code would also take care if there are multiple blanks

 

data a;
input name $ 1-30;
datalines;
i   am vinayak more
asif shaikh   is my friend
;
run;

 

 

data new(drop = len c char_val last_char_val);
length last_char_val $1. ;
retain last_char_val;
set a;
len=length(name);
c=1;word_count=0;
do while(c<=len);
char_val=substr(name,c,1);
if char_val eq ' ' and last_char_val ne ' ' then word_count +1;
c+1;
last_char_val=char_val;
end;
word_count +1;
run;

Ksharp
Super User

Why you post it at IML forum, Another DO LOOP is using CALL PRXNEXT().

 

data a;
input name $ 1-30;
datalines;
i am vinayak more
asif shaikh is my friend
;
run;
data want;
 set a;
 i=1;count=0;
 temp=scan(name,i);
 do while(not missing(temp));
  count+1;
  i+1;
  temp=scan(name,i);
 end;
drop i temp;
run;
Ksharp
Super User

OK. I just figure out IML way.

 

data a;
input name $ 1-30;
datalines;
i am vinayak more
asif shaikh is my friend
;
run;
proc iml;
use a;
read all var{name};
close;

count=j(nrow(name),1,0);
do i=1 to nrow(name);
token=substr(name[i],1:length(name[i]),1);
count[i]=sum(token=' ')+1;
end;
print name count;
quit;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

And why "don't use countw"?  I would think that a function specifically designed and written for the purpose would be better than any other solution?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 6 replies
  • 1196 views
  • 5 likes
  • 4 in conversation