新建文件夹
%sysexec md "G:\EC\KIE&month" & exit;
/***********************new folder*****************************************/
%let day=20991231;
data a;
newdirectory=dcreate(strip("&day."),"X:\BOBJ\Source\CACR\D16SRC");
newdirectory=dcreate(strip("&day."),"X:\BOBJ\Source\CACR\D16DB");
newdirectory=dcreate(strip("&day."),"X:\BOBJ\Source\CACR\DLM");
run;
记录日志
DM 'LOG;log;FILE "P:\EC\data_summary\&month\summary.log" append;';
/*****output log***/
dm 'LOG;file" C:\archive\mylog.log "';
/*****only output log to file***/
proc printto
log="C:\archive\mylog.log" new;
run;
/*********************** import & EXPORT (dde)**********************************/
dde export
%macro dde(excel=,sheet=,data=,r1=,r2=,c1=,c2=,out=);
filename dde_out dde "Excel|[&excel..xlsb]&sheet!R&r1.C&c1.:R&r2.C&c2." notab lrecl=2048;
data _null_;
set &data.;
file dde_out dsd dlm='09'x ;
put &out;
run;
%mend;
%dde(excel=Channel Dashboard template_CN, sheet=Channels vs Txn Types, data=channel.channel_all_fin, r1=7,r2=22,c1=3,c2=10,out=Internet ATMON ATMOFF ATM Branch IVR Contact_Centre SMS);
%Macro Exl2DS(ExlFile=,outdat=,dbms=,getnames=,textsize=,sheet=);
Proc import dbms = &dbms out = &outdat datafile = &ExlFile replace;
getnames = &getnames; /*导入源文件字段名作为SAS数据集的字段名*/
scantext = Yes; /*将源文件中各列值的最长长度作为该列在SAS中的字段长度。*/
usedate = Yes; /*对于包含日期字段的源文件字段,在SAS中只保留DATE值,并以DATE.格式存储。*/
scantime = Yes; /*对于源文件中只有time值的列,自动以时间格式(TIME)存储*/
mixed = Yes; /*若某一列中包含数值型和字符型变量,将数值型按照缺省值处理。若选的是YES则是将数值型转换成字符型存储,默认为NO*/
textsize = &textsize; /*源文件每一个单元格长度最大值*/
sheet = "&sheet.";
Run;
%mend Exl2DS;
%Macro DS2Exl(indat=,ExlFile=,sheet=);
PROC EXPORT DATA= &indat.
OUTFILE= &ExlFile.
DBMS=EXCEL2000 REPLACE;
SHEET=&sheet.;
run;
%Mend;
Proc Import:
datafile:要导入的文件
out:输出的数据集
dbms:文件类型,可以是tab csv dlm excel2000等等
replace:是否替换已经存在的数据集
三个子句(其中delimiter只针对dbms=dlm的情况):
delimiter:制定分隔符
getnames:是否从文件首行读入变量名
datarow:从那一行开始读入数据
如:
proc import datafile='c:\test.csv' out=test dbms=dlm replace;
delimiter=',';
getnames=no;
datarow=2;
run;
proc export data= work.X_SELL_&day2.
outfile= "X:\SAS_report\1401224\Monthly_013_CC_X_sell_report\Report\X_SELL_&day2..csv"
dbms=CSV replace;
run;
用SAS读取外部数据(用data步导入自由格式的外部数据)
Data 数据集名;
Infile "路径\表名" DLM=’characters’ <missover><firstobs=x>;
Input <@k> var1:定义的变量为字符型。
• Format:定义变量格式。
• M-n:读取m-n位。
• @:停顿符。
• @@:继续读取。
• /:换行符。
• DSD选项
注:
以从左到右的顺序读取数据。
所有变量默认长度为8位,如读入数据超出8位,需在之前定义
例如:
data profile;
infile "E:\Vichy\Vichy_Profile.csv" delimiter=',' dsd missover firstobs=2;
format CustomerID $50. Gender $11. Mobile_Phone $20. ;
input CustomerID $ Gender $ Mobile_Phone $ age;
run;
- 导出CSV:
proc export data=result
outfile='D:\Lancome_ec\报表\sampler.csv' dbms=csv replace;
run;
- 导入CSV:
%macro importcsv(dataset,path);
proc import datafile=&path out=&dataset;run;
%mend;
%importcsv(dataset=listwave,path="D:\Lancome_ec\报表\listwave.csv");
- 导入excel:
方法1:
PROC IMPORT OUT= WORK.recommend_rule
DATAFILE= "E:\EC\kie\ff-test\recommend_rule.xlsx"
DBMS=EXCEL REPLACE;
SHEET="Sheet1$";
GETNAMES=YES; /*指出第一行是否有字段名*/
MIXED=YES;/*同时读入同一字段的字符型和数值型变量*/
SCANTEXT=YES;/*会自动扫描,以最大的宽度作为改列字符变量的宽度。如果SCANTEXT=NO,则在不设定TEXTSIZE的情况下,默认长度为255。*/
RUN;
方法2:
libname lr "E:\Vichy\NPS_Mobile\薇姿1004_1010.xlsx";
data nps_hcr;set lr."sheet1$"n;run;
libname lr clear;
- 导出excel:
方法1:
PROC EXPORT DATA=&input
OUTFILE= "E:\EC\kie\ff-test\result\&output..xls"
DBMS=EXCEL REPLACE label;
SHEET="sheet1";
RUN;
方法2:
libname lib excel "C:\Users\eve\Desktop\test\time_test.xlsx";
proc datasets lib=lib kill;run;
data lib.sheet1(dblabel=YES);set time;run;
libname lib clear;
/****************excel多表导出在同一工作簿中***************/
方法1:
%macro report(input,output);
PROC EXPORT DATA=&input
OUTFILE= "E:\EC\kie\ff-test\定期项目\TMALL先试后买\Review\review&EC_date..xlsx"
DBMS=EXCEL REPLACE label;
SHEET="&output.";
RUN;
%mend;
%report(S2p_p2,先试后买推荐结果);
%report(P2p_p2,正装推荐结果);
%report(Productnum,正装回购人数);
%report(Samplenum,先试后买回购人数);
%report(p_time,正装回购参考日期);
%report(s_time,先试后买回购参考日期);
方法2:
libname xlout excel "E:\EC\kie\ff-test\定期项目\TMALL先试后买\Review\review&EC_date..xlsx";
proc datasets lib=xlout kill;run;
data xlout.先试后买推荐结果;set S2p_p2;run;
data xlout.正装推荐结果;set P2p_p2;run;
data xlout.正装回购人数;set Productnum;run;
data xlout.先试后买回购人数;set Samplenum;run;
data xlout.正装回购参考日期;set p_time;run;
data xlout.先试后买回购参考日期(dblabel=yes);set s_time;run;/*输出标签*/
libname xlout clear;
/********************************************************************/
- 输出由freq出的类别的频数和百分比构成的表
ods csv file="G:\EC\KIE\online datamart\&date\datamart_对比.csv";
/*输出datamart_对比表,总表中control和test包括以下6个量*/
proc freq data=t_test;table cluster;run;
proc freq data=c_control;table cluster;run;
proc freq data=t_test;table auscat;run;
proc freq data=c_control;table auscat;run;
proc freq data=t_test;table Frequency_online;run;
proc freq data=c_control;table Frequency_online;run;
ods csv close;
/***************导入外呼名单excel***************/
%let output=calllist;
%let path="E:\Vichy\Vichy Report\M89.xlsx";
%let sheet="data$"n;
%macro importexcel(dataset,sheet,path);
proc import
out=&dataset datafile=&path dbms=excel replace;
sheet=&sheet;
GETNAMES=YES;
MIXED=YES;
SCANTEXT=YES;
USEDATE=NO;/* 使用日期格式变量*/
SCANTIME=YES;
RUN;
%mend;
%importexcel(dataset=&output,sheet=&sheet,path=&path);
/**********读取外部原始数据**************/
1、读取TXT文件
data TEMP;
infile '/folders/myfolders/emp_data.txt';
input empID empName $ Salary Dept $ DOJ date9. ;
format DOJ date9.;
run;
PROC PRINT DATA=TEMP;
RUN;
2、读取CSV文件
data TEMP;
infile '/folders/myfolders/emp.csv' dlm=",";
input empID empName $ Salary Dept $ DOJ date9. ;
format DOJ date9.;
run;
PROC PRINT DATA=TEMP;
RUN;
3、读取Excel文件
FILENAME REFFILE
"/folders/myfolders/TutorialsPoint/emp.xls"
TERMSTR=CR;
PROC IMPORT DATAFILE=REFFILE
DBMS=XLS
OUT=WORK.IMPORT;
GETNAMES=YES;
RUN;
PROC PRINT DATA=WORK.IMPORT RUN;