saspy:将大型 SAS table 写入本地 csv

saspy: write large SAS table to local csv

SASData 对象有 to_csvto_df_CSV 方法,但这两个方法都写入主机上的位置(SAS 会话为 运行)。有没有办法通过远程连接将大型 SAS 数据 table 写入本地计算机上的 .CSV? saspy Github 页面上的选项是通过 to_df 获取 DataFrame,然后将其写入 .CSV,但这在我的情况下不起作用,因为 SAS table 是比内存大。我必须分块读写吗?

使用 saspy V2.4.3,您可以尝试 to_csv() 在 SAS 服务器上创建 csv 文件,然后使用 2.4.3 中新增的 download() 将 csv 拉到你的本地文件系统。

这是我建议的一个例子。

tom64-3> python3.5
Python 3.5.5 (default, Feb  6 2018, 10:56:47)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import saspy
>>> sas = saspy.SASsession(cfgname='iomjwin')
SAS Connection established. Subprocess id is 3681

No encoding value provided. Will try to determine the correct encoding.
Setting encoding to cp1252 based upon the SAS session encoding value of wlatin1.
>>> sas
Access Method         = IOM
SAS Config name       = iomjwin
WORK Path             = C:\Users\sastpw\AppData\Local\Temp\SAS Temporary Files\_TD20052_d10a626_\Prc4\
SAS Version           = 9.04.01M4P11142016
SASPy Version         = 2.4.3
Teach me SAS          = False
Batch                 = False
Results               = Pandas
SAS Session Encoding  = wlatin1
Python Encoding value = cp1252
SAS process Pid value = 20052


>>> cars = sas.sasdata('cars', 'sashelp')
>>> cars.head()
    Make           Model   Type Origin DriveTrain   MSRP  Invoice  EngineSize  \
0  Acura             MDX    SUV   Asia        All  36945    33337         3.5
1  Acura  RSX Type S 2dr  Sedan   Asia      Front  23820    21761         2.0
2  Acura         TSX 4dr  Sedan   Asia      Front  26990    24647         2.4
3  Acura          TL 4dr  Sedan   Asia      Front  33195    30299         3.2
4  Acura      3.5 RL 4dr  Sedan   Asia      Front  43755    39014         3.5

   Cylinders  Horsepower  MPG_City  MPG_Highway  Weight  Wheelbase  Length
0          6         265        17           23    4451        106     189
1          4         200        24           31    2778        101     172
2          4         200        22           29    3230        105     183
3          6         270        20           28    3575        108     186
4          6         225        18           24    3880        115     197
>>> cars.to_csv(sas.workpath+'\cars.csv')
11                                                                                                                       The SAS System                                                                                         11:15 Tuesday, February 19, 2019


99
100        options nosource;


NOTE: The file X is:
      Filename=C:\Users\sastpw\AppData\Local\Temp\SAS Temporary Files\_TD20052_d10a626_\Prc4\cars.csv,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=19Feb2019:11:16:58,
      Create Time=19Feb2019:11:16:58

NOTE: 429 records were written to the file X.
      The minimum record length was 68.
      The maximum record length was 123.
NOTE: There were 428 observations read from the data set SASHELP.CARS.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds


428 records created in X from SASHELP.CARS.


NOTE: "X" file was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
      real time           1.49 seconds
      cpu time            0.21 seconds


>>> res = sas.download('/u/sastpw', r'C:\Users\sastpw\AppData\Local\Temp\SAS Temporary Files\_TD20052_d10a626_\Prc4\cars.csv')
>>> print(res['LOG'])
19                                                                                                                       The SAS System                                                                                         11:15 Tuesday, February 19, 2019


278
279        filename _sp_updn 'C:\Users\sastpw\AppData\Local\Temp\SAS Temporary Files\_TD20052_d10a626_\Prc4\cars.csv' recfm=F encoding=binary lrecl=4096
280
281
20                                                                                                                       The SAS System                                                                                         11:15 Tuesday, February 19, 2019


284
21                                                                                                                       The SAS System                                                                                         11:15 Tuesday, February 19, 2019


287
288        filename _sp_updn;
NOTE: Fileref _SP_UPDN has been deassigned.
289
290
>>>
SAS Connection terminated. Subprocess id was 3681
tom64-3> ll /u/sastpw/cars.csv
-rw-r--r-- 1 sastpw r&d 38142 Feb 19 11:18 /u/sastpw/cars.csv
tom64-3>
tom64-3> head /u/sastpw/cars.csv
Make,Model,Type,Origin,DriveTrain,MSRP,Invoice,EngineSize,Cylinders,Horsepower,MPG_City,MPG_Highway,Weight,Wheelbase,Length
Acura,MDX,SUV,Asia,All,",945",",337",3.5,6,265,17,23,4451,106,189
Acura,RSX Type S 2dr,Sedan,Asia,Front,",820",",761",2,4,200,24,31,2778,101,172
Acura,TSX 4dr,Sedan,Asia,Front,",990",",647",2.4,4,200,22,29,3230,105,183
Acura,TL 4dr,Sedan,Asia,Front,",195",",299",3.2,6,270,20,28,3575,108,186
Acura,3.5 RL 4dr,Sedan,Asia,Front,",755",",014",3.5,6,225,18,24,3880,115,197
Acura,3.5 RL w/Navigation 4dr,Sedan,Asia,Front,",100",",100",3.5,6,225,18,24,3893,115,197
Acura,NSX coupe 2dr manual S,Sports,Asia,Rear,",765",",978",3.2,6,290,17,24,3153,100,174
Audi,A4 1.8T 4dr,Sedan,Europe,Front,",940",",508",1.8,4,170,22,31,3252,104,179
Audi,A41.8T convertible 2dr,Sedan,Europe,Front,",940",",506",1.8,4,170,23,30,3638,105,180
tom64-3>