02-14-2013 02:40 PM
Trying to accomplish a couple of things with a list of items.
This is my variable (separated by commas) :
%let macrolist = '79.01', '79.02', '79.03', '79.04';
The first thing I'm trying to do is count the number of items in the list. So, I did find
this code that does help in counting the items, but since I have commas in the list, I get an error (more parameters found
than defined. So, the first question is how do I count each item individually even though the comma is in there ?:
%macro wordcount (macrolist);
* Count the number of items in the list;
%let count = 0;
%do %while(%qscan( ¯olist, &count+1, %str( )) ne %str( ));
%let count = %eval(&count + 1);
Then, I have this code to create macro variables for each item in the list. Will this work even those commas separate the items ?:
%do i = 1 %to %wordcount(¯olist);
%let cvar&i = %scan(¯olist, &i, %str( ));
so the final outcome should be this:
cvar1 = '79.01'
cvar2 = '79.02'
cvar3 = '79.03'
cvar4 = '79.04'
Thank you in advance for the help.
02-14-2013 03:00 PM
Why bother counting? Or creating the macro variables at all? I'm guessing your looking for a way to loop through a list and do something for each of the variables?
Hopefully the following helps:
*For looping through a list
%do %while (%scan(&varlist, &i, ",") ^=%str());
%let var=%scan(&varlist, &i, " ");
02-14-2013 03:33 PM
This may not be a complete list, but it will move you in the right direction.
Within the definition of %WORDCOUNT, replace ¯olist with %superq(macrolist).
Within the definition of %ASSIGN, make the same change in two places.
Within the %LET statement, change the delimiter for %SCAN from %str( ) to %str(, ) so that both commas and blanks will be delimiters. Note that this might not be the right thing to do for any and all applications, but it's necessary here.
That might take care of it.
02-14-2013 05:58 PM
Thanks for the suggestions, but I'm still having difficulty.
not sure where I'm going wrong, tried both possiblities:
%do %while (%scan(¯olist, &i, ",") ^=%str());
%let var=%scan(¯olist, &i, " ");
it end it up counting only 2 records.
Then I tried this
/* Count the number of words in;*/
%do %while(%qscan(%superq(¯olist),&count+1,%str(, )) ne %str(, ));
%let count = %eval(&count+1);
%do i=1 %to %wordcount(%superq(¯olist));
%let cvar&i=%scan(%superq(¯olist),&i,%str(, ));
Got error "invalid symbolic variable name".
Any suggestions ?
02-14-2013 09:46 PM
Sure. First, note that %superq assumes that you will pass the name of a macro variable. So the proper syntax for your application would omit the ampersand: %superq(macrolist)
Technically %superq(¯olist) says that ¯olist resolves into the name of the macro variable that you would like to quote ... but I've never seen an application where that is needed.
It seems like you are switching logic when counting, to use both blanks and commas as delimiters. But the loop should still end when %qscan returns a null string. So the loop would look like this:
%do %while (%qscan(%superq(macrolist), &count+1, %str(, )) ne );
The definition of %ASSIGN looks correct, once the ampersand is removed from ¯olist.
See how that turns out ... might be done at that point.