BookmarkSubscribeRSS Feed
suncawy
Obsidian | Level 7


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.

4 REPLIES 4
Reeza
Super User

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;

Astounding
PROC Star

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.

suncawy
Obsidian | Level 7

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.

Astounding
PROC Star

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.

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 7489 views
  • 0 likes
  • 3 in conversation