Search code examples
arrayssasglobal-variablessas-macro

How to Declare Global Array Variable in SAS?


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!


Solution

  • 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.)