BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Niugg2010
Obsidian | Level 7

Below is my macro:

%macro try(dsns=);

%let dsnnum=%sysfunc(countw("&dsns.", ' ,;:'));

%put &dsnnum.;

%do k=1 %to &dsnnum.

%let dsn=%scan(&dsns., &k., %str( ,));

%put &dsn.;

%end;

%mend ;

%try(dsns=%str(aaa, bbb, ccc))

 

For the first loop, I want it &dsn=aaa, second loop &dsn=bbb, third loop &dsn=ccc;

 

when I run above code, it produces some errors:

 

WARNING: Apparent symbolic reference K not resolved.

WARNING: Apparent symbolic reference K not resolved.

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric

operand is required. The condition was: &k.

ERROR: Argument 2 to macro function %SCAN is not a number.

 

 

Someone can help me out?

 

Thanks

George

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Niugg2010,

 

Adding the missing semicolon after

%do k=1 %to &dsnnum.

would help a lot.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Your first %LET statement has what appear to be unbalanced quotes.  Why are you using quotes anyway?

Why are you making your task harder by adding commas in the value you are passing to macro?

If it is a list of dataset names then just use spaces between the names.

%macro try(dsns);
  %local dsnnum k dsn;
  %let dsnnum=%sysfunc(countw(&dsns,%str( )));
  %put &=dsnnum ;
  %do k=1 %to &dsnnum ;
    %let dsn=%scan(&dsns., &k., %str( ));
    %put &=k &=dsn;
  %end;
%mend try ;

%try(dsns=aaa bbb ccc)
FreelanceReinh
Jade | Level 19

Hi @Niugg2010,

 

Adding the missing semicolon after

%do k=1 %to &dsnnum.

would help a lot.

Niugg2010
Obsidian | Level 7
Got it. Thanks

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1249 views
  • 0 likes
  • 3 in conversation