BookmarkSubscribeRSS Feed
waldo11
Fluorite | Level 6

I want a scan function within a macro that can pull last name of the third person in a list.

 

Example:

 

%put &full_name_list.

HARRY LEVY, ABEL JAMES, ABHISHEK SINGH, ADAM LINKS

 

%macro name_test();

%do array_counter = 1 %to &name_count.;
%let full_name = %SYSFUNC(SCAN(&FULL_NAME_LIST, &ARRAY_COUNTER, ','));
%end;
%mend;
%name_test();

 

I get the following error when I run this:

 

NOTE: One or more missing close parentheses have been supplied for the %SCAN function.
ERROR: Expected close parenthesis after macro function invocation not found.

 

When I have a list of one name (either first or last) then this works perfectly. But I want to be able to pull the full name out. Any ideas?

2 REPLIES 2
ballardw
Super User

1) The macro language has its own %scan function, so no need to us %sysfunc.

2) in general passing lists with comma's causes problems because the first comma encountered is going to be treated as a delimiter between successive parameters.

3) really not a good idea to have macro variables just appear in the middle of the code you should have a parameter for the list

%macro name_list (full_name_list);

4) And passing a parameter with embedded commas would require use of

%name_test( %str(HARRY LEVY, ABEL JAMES, ABHISHEK SINGH, ADAM LINKS) );

 

 

5) No particular nasty code needed:

 

%let list=HARRY LEVY, ABEL JAMES, ABHISHEK SINGH, ADAM LINKS;

%let lastthird= %scan("&list",6);
%put &lastthird;

Normally I would not put the &list in quotes but the comma's are also an issue for any macro function.

 

Tom
Super User Tom
Super User

You tried to execute this mess:

%let full_name_list=%sysfunc(scan(HARRY LEVY, ABEL JAMES, ABHISHEK SINGH, ADAM LINKS,1,','));

So SAS is complaining that you have given the SCAN() function 6 arguments.  That function does not have 6 arguments.

 

If you have comma delimited list you will need to use macro quoting to protect the commas from becoming part of the command syntax. Also don't include quotes in the list of delimiters or else the SCAN(), or %SCAN(), function will use them as delimiters.

 

If you want to find an item in the list use something like this instead:

%let full_name=%scan(%superq(full_name_list),&ARRAY_COUNTER,%str(,));

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 366 views
  • 0 likes
  • 3 in conversation