0

I have a SAS macro string defined as:

%let datasets =
lib.d1
lib.d2
lib.d3
;

The string is a macro variable. Each element is a dataset with the library name. The elements are separated by line breaks. Now I want to split the elements and iterate through them one by one. I would like to use %scan for this but I don't know how to setup %scan so it can use delimiter of line break rather than the dot character which is in the default delimiter list. Any suggestions?

Steve
  • 4,935
  • 11
  • 56
  • 83
  • Don't forget you can always check the documentation to see what parameters a function will take: http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#z3514scan.htm – Robert Penridge Nov 12 '15 at 18:06

2 Answers2

1

You need to tell %SCAN() what delimiter(s) to use if you do not want to use the default delimiters. In this case you want to use a space. Note that there will not be any "line breaks" in your macro variable if you create it that way.

 %put %scan(&datasets,1,%str( ));
Tom
  • 47,574
  • 2
  • 16
  • 29
0

Although they're separated by line breaks in your code, they are still treated as spaces.

%LET DATASETS = 
lib.d1
lib.d2
lib.d3
;

%MACRO SPLIT(STRING) ;
  %DO I = 1 %TO %SYSFUNC(countw(&STRING,%STR( ))) ;
    %PUT %SYSFUNC(scan(&STRING,&I,%STR( ))) ;
  %END ;
%MEND ;

%SPLIT(&DATASETS) ;
Chris J
  • 7,549
  • 2
  • 25
  • 25
  • I was thinking along the same lines as the solution you've suggested. Is there a reason you used `scan()` as opposed to `%scan()` which also works? – Amir Nov 12 '15 at 13:27
  • No particular reason, just personal preference, and there are some functions which SAS hasn't predefined as a macro equivalent. – Chris J Nov 12 '15 at 19:21