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

Is there a way we can extract number of words from a macro variable?

 

For example :

%let var = Balance Years_Since_Active Field1 Field2;

I want to extract first 3 from this list.

 

Note : In reality, the list is really big. It contains almost 800 words. I wanted to extract first 100 words. After that, I would like to extract from 101 to 200 and so on. Hence, the solution should be dynamic in such a manner user can specify lower limit i.e. 1 and upper limit i.e. 100. = SCAN(&var, (1:100), ' '). 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Given that the same word never appears twice ...

 

It's probably a little easier to work with a DATA step instead of macro language.  But all of this logic could be done with a macro if necessary.

 

%let first_word = 5;

%let last_word = 10;

%let var = some list of a great many words that do not contain any repeats;

data _null_;

p1 = scan("&var", &first_word, ' ');

p2 = scan("&var", &last_word, ' ');

if p1 > ' ' and p2 > ' ';

position1 = findw("&var", strip(p1));

position2 = findw("&var", strip(p2));

subset_of_words = substr("&var", position1, position2 - position1 + length(p2));

call symputx('subset_of_words', subset_of_words);

run;

 

%put &subset_of_words;

 

It's untested code, but should be fine.  At least the number of tools used is fairly small if you need to review any of them.

 

Good luck.

View solution in original post

5 REPLIES 5
SASKiwi
PROC Star
%let var = Balance Years_Since_Active Field1 Field2;
%let word_count = %sysfunc(countw(&var));
%put word_count = &word_count;
Astounding
PROC Star

Can you guarantee that the same word will never appear twice within the list?  That could impact what tools will work.

Ujjawal
Quartz | Level 8

Yes the same word will never come twice within the list.

Astounding
PROC Star

Given that the same word never appears twice ...

 

It's probably a little easier to work with a DATA step instead of macro language.  But all of this logic could be done with a macro if necessary.

 

%let first_word = 5;

%let last_word = 10;

%let var = some list of a great many words that do not contain any repeats;

data _null_;

p1 = scan("&var", &first_word, ' ');

p2 = scan("&var", &last_word, ' ');

if p1 > ' ' and p2 > ' ';

position1 = findw("&var", strip(p1));

position2 = findw("&var", strip(p2));

subset_of_words = substr("&var", position1, position2 - position1 + length(p2));

call symputx('subset_of_words', subset_of_words);

run;

 

%put &subset_of_words;

 

It's untested code, but should be fine.  At least the number of tools used is fairly small if you need to review any of them.

 

Good luck.

Reeza
Super User
It's possibly more efficient to toss the list into a data step and then query it via either a data step or proc sql. At least it might be easier to understand 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2589 views
  • 0 likes
  • 4 in conversation