How to Declare Global Array Variable in SAS?

6k views Asked by At

I'm new to SAS and spinning my wheels. The SAS documentation and other Google searches have not helped me figure this out. How can I declare a global array variable that I can use in various procedures to loop through the contents?

Here is what I've tried:

%let fileArray = array{*} $32 file1-file4  ('ce_abcdef_filedetail1' 'ce_abcdef_filedetail2' 'ce_abcdef_filedetail3' 'ce_abcdef_filedetail4' );

/* Loop through each file and run the macro*/
do i = 1 to dim(fileArray);
     %analyze_file(FILENAME=&fileArray[i], PATH=&path, OUTPUT=&output)
end;

I need it to pass the filename that I specify in the global array. Thanks for any help!

2

There are 2 answers

6
Joe On BEST ANSWER

What you're trying to do is basically to use a data driven programming approach to drive your macros. Good for you! However, you can't do it directly the way you are trying to. While you could use a macro array the way Yukclam9 mentions, there's an easier way.

SAS doesn't use arrays the way r uses vectors or matrices: SAS uses datasets, though, and you can do a lot of the same things.

Put your filenames into a dataset - perhaps they're already there, in an excel file or something? Let's put it here in datalines, in case they're not.

data filenames;
  input filename :$32.;
  datalines;
ce_abcdef_filedetail1
ce_abcdef_filedetail2 
ce_abcdef_filedetail3 
ce_abcdef_filedetail4
;;;;
run;

Now, you want to get them into a macro call. Sweet, we have a lot of ways of doing that. This is the quickest.

proc sql;
  select cats('%analyze_file(FILENAME=',filename,", PATH=&path, OUTPUT=&output)")
    into :mcalllist separated by ' '
    from filenames;
quit;

CATS just concatenates and strips spaces. I leave &path and &output alone as it looks like they're global macro variables - of course if they're also variable, you could include them the same way.

Now &mcalllist is a global macro variable that stores your four macro calls (or however many were in that dataset, one per row)! You just execute

&mcalllist

and presto, it calls your macro. You can also use call execute or construct a file and %include it to do much the same thing, with some different limitations. (This one has a maximum of 65k characters or so, for example.)

2
yukclam9 On

Check this informative article: Macro Array Statement

What you could do would be to create the array by

%array ( younameit, values = 'ce_abcdef_filedetail1' 'ce_abcdef_filedetail2' ...)

note that the term "values" is fixed and values after the "=" is up to you

and do it with %do_over

%macro read_file(fileArray);
  %analyze_file(FILENAME=&fileArray, PATH=&path, OUTPUT=&output)
  %end;
%mend read_file;

%do_over(younameit, macro=read_file)

The result would trigger macro read_file('ce_abcdef_filedetail1' ), read_file('ce_abcdef_filedetail2') and the remained respectively. I would also suggest you to put the quote ( double quote to trigger macro) into the statement but not with the macro variable.