Skip to content

Commit fe16a43

Browse files
authored
Create SAS_export_multiple_workbook_worksheet.sas
1 parent f975404 commit fe16a43

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*This program is designed to show how you can automate the creation of workbooks in Excel.
2+
The criteria is to create a workbook for every Origin value in the CARS data set and
3+
in each workbook, create a separate sheet for each make.
4+
For example, it will create a workbook for Asia, with sheets for all the makes include Kia, Honda, Hyundai, etc.
5+
6+
Author: F.Khurshed
7+
Date: 2018-04-05
8+
*/
9+
10+
11+
%*Generate sample data to work with here;
12+
proc sort data=sashelp.cars out=cars;
13+
by origin make;
14+
run;
15+
16+
*Close other destinations to improve speed;
17+
ods listing close;
18+
ods html close;
19+
20+
*macro that exports to file with Origin in file name and a
21+
sheet for each make. The number of origins or makes is not
22+
needed ahead of time;
23+
24+
%macro export_origin(origin=);
25+
26+
%*filename for export, set style for fun and add label for each sheet;
27+
ods excel file="C:\_localdata\Cars_&origin..xlsx"
28+
style = meadow
29+
options(sheet_interval='bygroup'
30+
sheet_label='Make');
31+
32+
*generate a sheet for each make (by make);
33+
proc print data=cars noobs label;
34+
where origin = "&Origin";
35+
by make;
36+
run;
37+
38+
%*close excel file;
39+
ods excel close;
40+
41+
%mend;
42+
43+
*calls macro for each origin in file.
44+
number of origins doesn't need to be known ahead of time;
45+
46+
data _null_;
47+
set cars;
48+
by origin;
49+
50+
if first.origin then do;
51+
*create macro call;
52+
str = catt('%export_origin(origin=', origin, ');');
53+
*call macro;
54+
call execute(str);
55+
56+
end;
57+
58+
run;
59+
60+
%*reopens output destinations;
61+
ods html;
62+
ods listing;

0 commit comments

Comments
 (0)