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