【问题标题】:SAS Using a DO loop to read in multiple files but need to append the table in each iteration to prevent losing dataSAS使用DO循环读取多个文件但需要在每次迭代中追加表以防止丢失数据
【发布时间】:2016-06-17 19:49:14
【问题描述】:

当我运行宏将来自多个文件的数据输入到一个 SAS 表中时,它会在下一次迭代开始时替换正在读取的每个数据集。因此,我在最终表格中剩下的唯一数据是我使用循环读取的最后一个文件。如何在每次读取迭代结束时附加输出表,以防止每次移动到下一个文件时丢失数据?

当前代码:

  %let type=40;

%let year=2015;

%let months =07;

%let days = 01 02 03 04 05  06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31;

%let filetype=.csv;



%macro import_files;


                    %do K=1 %to 31; /*day*/

                           /*Scans through Macro Variable Arrays to find current month, year and letter group*/

                           %let nmbr = %scan(&type,1);

                           %let yr=%scan(&year,1);

                           %let mnth=%scan(&months,1);

                           %let day=%scan(&days,&K);



                           /*Creates string variables for Export and import locations of files using macro loop variables*/

                           %let exportfile = "C:\Users\wwn\Documents\HistoricalRateTesting\Top&nmbr&yr&mnth&day&filetype";

                           %let src = "\\2119\Raw_Data\Transposed_Canvass\Top&nmbr&yr&mnth&day&filetype";


                           /*Imports All files in that Alpha, Year and Month Group*/

                           DATA work.RAW_DATA;

                                INFILE &src firstobs=3 LRECL=1000 DELIMITER=',' TRUNCOVER DSD;
/*                              informat arv_dt mmddyy10. shop_dttm DATETIME18. arv_tm time5. rtrn_tm time5.;*/
/*                              format shop_dttm DATETIME18. arv_tm time5. rtrn_tm time5.;*/

                                INPUT

                                     city_cd          : $CHAR3.

                                     rtrn_city_cd     : $CHAR3.

                                     shop_car_type_cd : $CHAR4.

                                     shop_rt_categ    :  $CHAR2.

                                     shop_rt_type     :  $CHAR1.

                                     lor              : ?? BEST2.

                                     arv_tm           : ?? TIME5.

                                     rtrn_tm          : ??TIME5.

                                     arv_dt           : ?? MMDDYY10.

                                     shop_dttm        : ANYDTDTM.

                                     Brand_ZE         : ?? BEST10.2

                                     Brand_ZI         : ?? BEST10.2

                                     Brand_ZL         : ?? BEST10.2

                                     Brand_ZD         : ?? BEST10.2

                                     Brand_AL         : ?? BEST10.2

                                     Brand_ZR         : ?? BEST10.2

                                     Brand_ET         : ?? BEST10.2

                                     Brand_ZT         : ?? BEST10.2

                                     Brand_AD         : ?? BEST10.2

                                     Brand_ZA         : ?? BEST10.2

                                     Brand_EZ         : ?? BEST10.2

                                     Brand_SX         : ?? BEST10.2

                                     Brand_FX         : ?? BEST10.2

                                     Brand_FF         : ?? BEST10.2;

                RUN;

                                    %end;

                                     %mend import_files;
                                     %import_files;

【问题讨论】:

标签: loops macros sas append


【解决方案1】:

似乎所有文件都具有相同的布局,从 SAS 8.2 开始,您有了一个不错的技巧。

首先声明所有文件的列表。 如果您的目录包含您要读取的所有文件 - 并且只有那些文件:

filename myfiles ("\\2119\Raw_Data\Transposed_Canvass\*"); 

然后在INFILE 语句中使用声明myfiles

DATA work.RAW_DATA;  
    INFILE myfiles firstobs=3 LRECL=1000 DELIMITER=',' TRUNCOVER DSD;
/* ... */
RUN;

如果您的文件夹包含其他文件,您首先必须使用小宏来确定要读取的文件以扩展文件列表以便拥有;

filename myfiles ("path_to_file1","path_to_file2","path_to_file3"...);

【讨论】:

  • 是否处理所有文件中的标题?您可以在 infile 语句中包含通配符,而在文件名语句中不需要这样做。
猜你喜欢
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多