DATA Step, Macro, Functions and more

Macro variable list separted by a comma

Reply
Contributor
Posts: 24

Macro variable list separted by a comma


Hi,

   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;

    %local count;

     %let count = 0;

      %do %while(%qscan( &macrolist, &count+1,   %str( )) ne %str( ));

      %let count = %eval(&count + 1);

%end

&count

%mend wordcount;

  Then, I have this code to create macro variables for each item in the list.  Will this work even those commas separate the items ?:

%macro assign;

        %do i = 1 %to %wordcount(&macrolist);

              %let cvar&i = %scan(&macrolist, &i,  %str( ));

        %end;

%mend assign;

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.

Super User
Posts: 19,877

Re: Macro variable list separted by a comma

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

%let i=1;

%do %while (%scan(&varlist, &i, ",") ^=%str());

%let var=%scan(&varlist, &i, " ");   

*Increment counter;

%let i=%eval(&i+1);

%end;

Super User
Posts: 5,518

Re: Macro variable list separted by a comma

This may not be a complete list, but it will move you in the right direction.

Within the definition of %WORDCOUNT, replace &macrolist 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.

Good luck.

Contributor
Posts: 24

Re: Macro variable list separted by a comma

Thanks for the suggestions, but I'm still having difficulty.

not sure where I'm going wrong, tried both possiblities:

%macro wordcount(macrolist);
   %let i=1;

     %do %while (%scan(&macrolist, &i, ",") ^=%str());

            %let var=%scan(&macrolist, &i, " ");   

            %let i=%eval(&i+1);

    %end;
%mend wordcount;

%put %wordcount(&i);

it end it up counting only 2 records.

Then I tried this

%macro wordcount(macrolist);
/* Count the number of words in;*/
%local count;
%let count=0;
   %do %while(%qscan(%superq(&macrolist),&count+1,%str(, )) ne %str(, ));
        %let count = %eval(&count+1);
  %end;
&count
%mend wordcount;

%macro assign;
     %do i=1 %to %wordcount(%superq(&macrolist));
          %let cvar&i=%scan(%superq(&macrolist),&i,%str(, ));
      %end;

%mend assign;

%assign;

%put &cvar1;
%put &cvar2;
%put &cvar3;
%put &cvar4;

Got error  "invalid symbolic variable name".

Any suggestions ?

Thanks.

Super User
Posts: 5,518

Re: Macro variable list separted by a comma

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(&macrolist) says that &macrolist 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 &macrolist.

See how that turns out ... might be done at that point.

Ask a Question
Discussion stats
  • 4 replies
  • 2438 views
  • 0 likes
  • 3 in conversation