-
Notifications
You must be signed in to change notification settings - Fork 22
/
SAS2POSTGRE.sas
75 lines (61 loc) · 1.87 KB
/
SAS2POSTGRE.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
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Author: Edwin Hu
Date: 2013-05-24
# SAS2POSTGRE #
## Summary ##
Exports SAS dataset to PostgreSQL database
## Variables ##
- lib: default library (USER)
- dsetin: input dataset
- server: Postgresql server address (localhost)
- port: Postgresql port number (5432)
- user: Postgresql user
- pass: Postgresql user password
- db: Postgresql database
- format: format statement for SAS dataset columns
- rename: rename statement for SAS dataset columns
- debug: debug mode (n)
## Usage ##
```
%IMPORT "~/git/sas/SAS2POSTGRE.sas";
%SAS2POSTGRE(lib=USER,dsetin=&syslast.,
server=localhost, port=5432,
user=eddyhu, pass='asdf', db=wrds,
format=,rename=,debug=n);
```
*/
%MACRO SAS2POSTGRE(lib=USER,dsetin=&syslast.,
server=localhost, port=5432,
user=eddyhu, pass='asdf', db=wrds,
format=,rename=,debug=n);
%if %SUBSTR(%LOWCASE(&debug.),1,1) = n %then %do;
options sastrace=',,,d' sastraceloc=saslog;
%end;
* Connection string;
libname pgdb postgres server=&server. port=&port.
user=&user. password=&pass. database=&db. autocommit=no;
* Make a temp dataset with the correct data environment
and format columns to bulk load into Postgre;
data _data_f / view=_data_f;
set &lib..&dsetin.;
&format.
&rename.
run;
* Drop the table if it exists and bulk load the temp dataset;
ods listing close;
proc sql dquote=ansi;
drop table pgdb.&dsetin.;
create table pgdb.&dsetin.(
BULKLOAD=YES
BL_PSQL_PATH='psql'
BL_DELETE_DATAFILE=NO
BL_DEFAULT_DIR='/mnt/data/SASTemp/'
)
as select * from _data_f;
drop view _data_f;
quit;
* Close session;
LIBNAME pgdb CLEAR;
ods listing;
%put;%put Table &dsetin. created on &server. &db.;
%MEND SAS2POSTGRE;