SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Extract words from macro variable

Accepted Solution Solved
Reply
Regular Contributor
Posts: 183
Accepted Solution

Extract words from macro variable

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), ' '). 

 

 


Accepted Solutions
Solution
‎10-16-2015 05:01 PM
Super User
Posts: 5,507

Re: Extract words from macro variable

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


All Replies
Super User
Posts: 3,254

Re: Extract words from macro variable

%let var = Balance Years_Since_Active Field1 Field2;
%let word_count = %sysfunc(countw(&var));
%put word_count = &word_count;
Super User
Posts: 5,507

Re: Extract words from macro variable

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

Regular Contributor
Posts: 183

Re: Extract words from macro variable

Posted in reply to Astounding

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

Solution
‎10-16-2015 05:01 PM
Super User
Posts: 5,507

Re: Extract words from macro variable

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.

Super User
Posts: 19,814

Re: Extract words from macro variable

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 Smiley Happy
🔒 This topic is solved and locked.

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

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