DATA Step, Macro, Functions and more

How to get last word in given string or identify end of the string

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

How to get last word in given string or identify end of the string

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


Accepted Solutions
Solution
‎04-23-2015 05:34 PM
PROC Star
Posts: 7,363

Re: How to get last word in given string or identify end of the string

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)

View solution in original post


All Replies
Super User
Posts: 10,500

Re: How to get last word in given string or identify end of the string

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;

Solution
‎04-23-2015 05:34 PM
PROC Star
Posts: 7,363

Re: How to get last word in given string or identify end of the string

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)

Contributor
Posts: 37

Re: How to get last word in given string or identify end of the string

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!

PROC Star
Posts: 7,363

Re: How to get last word in given string or identify end of the string

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1210 views
  • 9 likes
  • 3 in conversation