hi,
I was trying to make macro where I one parameter passed in, I just wanted to find how many words are passed into this and what is length of each word.
Like-
calling macro-
%find_len(hi this dharmendra)
Output:
Total length of the str is(including spaces): 18
Total length of the str is(excluding spaces): 15
First str is- hi and and length is: 2
Second str is- this and lenght is: 4
Third str is- dharmendra and length is: 10
I have tried creating the macro but I am facing the biggest challenge is that how to get end of the string.
%macro find_len(text);
%let len_with_space=%length(&text);
%let len_without_space=%length((%SYSFUNC(COMPBL(&text)));
%local I;
%let I=1;
%do %UNTIL(last word of the str) ******here is the problem I do not know how to get last char or word or how identify end of the input str *************;
%let word=%scan(&text,I,)
I=%eval(I+1);
%put &I str is- &word and length is: %lenght(&word);
%end;
%mend find_len;
Thanks!!
Dharmendra
You could also keep with the approach you tried, but using the correct spelling of functions and using the right ones will help:
%macro find_len(text);
%let len_with_space=%length(&text);
%let len_without_space=%length(%SYSFUNC(COMPRESS(&text)));
%put Total length of the str is (including spaces): &len_with_space.;
%put Total length of the str is (excluding spaces): &len_without_space.;
%let I=1;
%do %while(%length(%scan(&text,&I.,' ')) gt 0);
%let word=%scan(&text,&I.,' ');
%put &I str is- &word and length is: %length(&word);
%let I=%eval(&I.+1);
%end;
%mend find_len;
%find_len(hi this dharmendra here)
You can get a count of words with
%sysfunc(countw(&string,' ,'))
The part ' ,' is saying what to use to separate words.
So
%let wordcount = %sysfunc(countw(&text,' ,'));
%do I = 1 %to &wordcount;
%let word = %scan(&text,&i, ' ,'));
%put &I str is- &word and length is: %length(&word);
%end;
You could also keep with the approach you tried, but using the correct spelling of functions and using the right ones will help:
%macro find_len(text);
%let len_with_space=%length(&text);
%let len_without_space=%length(%SYSFUNC(COMPRESS(&text)));
%put Total length of the str is (including spaces): &len_with_space.;
%put Total length of the str is (excluding spaces): &len_without_space.;
%let I=1;
%do %while(%length(%scan(&text,&I.,' ')) gt 0);
%let word=%scan(&text,&I.,' ');
%put &I str is- &word and length is: %length(&word);
%let I=%eval(&I.+1);
%end;
%mend find_len;
%find_len(hi this dharmendra here)
Hey Arthur,
Thank you for your reply on this, it helped me.
Just curious to know what is correct use of the Dot after the I variable?
I have always used Dot like working with something post fix like this
X1 X2 X3 X4
so i use X.&I
and I will start from 1 to 4 likewise.
Please help me understand this concept here?
Thank you!
The dot, as I used it, was simply a delimiter to indicate the end of a macro variable.
The documentation states: A period immediately following a macro variable reference acts as a delimiter. That is, a period at the end of a reference forces the macro processor to recognize the end of the reference. The period does not appear in the resulting text.
Most of the time it isn't needed, but sometimes it is absolutely essential.
In the case you mentioned, I would have used X&i.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.