Generating Multiple Excel Files

The macro is dynamic to generate multiple Excel files containing the frequency results for each unique BY-group. (e.g., Type).

PROC SQL SELECT code is embedded inside a macro in this SAS example.

The SELECT query processes the column (passed in the macro parameter) and then create a macro variable with the number of unique (distinct) value in the column, and a macro variable with the list of unique values separated with “-“

The macro uses the <a href="">PROC FREQ</a> procedure, an iterative macro %DO statement, a %SCAN function, and WHERE= data set option to dynamically send the results to an Excel spreadsheet for each BY-group.

options symbolgen symbolgen mprint;
        * Get the count of unique values in the column;
        proc sql noprint;
                        /* number of unique values */
            select count(distinct &col) into :mprodtype_cnt 
            from &ds order by &col;
            select distinct &col into :mprodtype_lst separated by 
                "-" /* list of product types */
                from &ds order by &col;

        %do j=1 %to &mprodtype_cnt;
            ods Excel file="&outpath/%SCAN(&mprodtype_lst,&j,-).xlsx" 
                style=styles.barrettsblue options(embedded_titles="yes");
            title "&col. - %SCAN(&mprodtype_lst,&j,-)";
            * Frequency of the column;

            proc freq data=&ds(where=(&col="%SCAN(&mprodtype_lst,&j,-)"));
                tables &col.;
ods Excel close;
%put &mprodtype_lst;
/******* END OF FILE *******/

%multExcelfiles(ds=SASHELP.CARS, col=TYPE,
Generating Multiple Excel Files

Download this source code.

Every week we'll send you SAS tips and in-depth tutorials



Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.