SAS日常使用的语句预定的python表达
1
导入数据
Sas:
filenameexport"C:\Users\Desktop\test.csv"encoding='utf-8';
PROCIMPORTout=raw.xingye_appl
datafile= export
dbms= csvreplace;
RUN;
Python:
Tmp=pd.read_csv('C:\\Users\\Desktop\\test.csv',dtype=object)
2
导出数据
Sas :
PROCEXPORTDATA=test
OUTFILE=" C:\Users\Desktop\test.csv"
DBMS=csv
label
REPLACE;
RUN;
Python:
Tmp.to_csv('C:\Users\Desktop\test.csv',index=False,encoding="utf-8")
3
过滤数据之后保留列
Sas:
datatmp;
setdf;
wherea="y";
keepa b;
run;
python:
tmp1=df[df['a']!='y'][['a','b']]
4
删除变量
Sas:
datadf;
setdf;
dropa b;
run;
python:
df=df.drop(['a','b'],axis=1)
5
重命名
Sas:
datatmp;
settmp;
renameaa_1=aa bb_1=bb;
run;
python:
tmp=tmp.rename(columns={'aa_1':'aa','bb_1':'bb'})
6
排序
Sas:
procsortdata=tmp;byb;run;
python:
tmp=tmp.sort_values(by = ['b'],axis =,ascending = True)
7
表连接
Sas
procsortdata=tmp;byaa;quit;
procsortdata=tmp1;byaa;quit;
datatmp2;
mergetmp(in=a) tmp1(in=b);
byaa;
ifa;
quit;
datatmp2;
mergetmp(in=a) tmp1(in=b);
byaa;
ifa and b;
quit;
python:
pd.merge(tmp,tmp1,how='left',on='aa')
pd.merge(tmp,tmp1,how='inner',on='aa')
8
变量分段
Sas
data tmp;
settmp;
if2000thennew_aa='[0, 2000)';
elseif200010000thennew_aa='[2000, 10000)';
elseif1000020000thennew_aa='[10000, 20000)';
elseif20000100000thennew_aa='[20000, 100000)';
run;
python
aa=[,2000,10000,20000,100000]
tmp['new_aa'] = pd.cut(tmp['new'], aa,right=False)
9
追加表
Sas:
procappendbase=tmp1data=tmp2;run;
python:
tmp1=tmp1.append(tmp2)
领取专属 10元无门槛券
私享最新 技术干货