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

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

4 REPLIES 4
ballardw
Super User

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;

art297
Opal | Level 21

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)

LittlesasMaster
Obsidian | Level 7

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!

art297
Opal | Level 21

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-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!

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