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), ' ').
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.
%let var = Balance Years_Since_Active Field1 Field2;
%let word_count = %sysfunc(countw(&var));
%put word_count = &word_count;
Can you guarantee that the same word will never appear twice within the list? That could impact what tools will work.
Yes the same word will never come twice within the list.
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.
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.
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.