SAS:导出多个 SAS 数据集并进行一些编辑到多个 csv 文件中

发布于 2025-01-16 01:57:38 字数 930 浏览 3 评论 0原文

我已经使用一段看似没有问题的代码已经有一段时间了,今天它以一种非常奇怪的方式停止工作。

我的文件夹中有几个 SAS 文件,我想将它们每个导出到 CSV 文件中,同时仅保留某些列并且如果满足某些条件。

文件结构是名为 data_(year) 的文件夹,里面有几个名为 file_(region number) 的文件。

%macro extract_data(year);
libname lib "c:\data_&year.";

%let file_names = 24 27 29;
%let nb_files = 3;

%do i=1 %to &nb_files.;

   %let next_file = %scan(&file_names, &i);

   data file&next_file. (keep = C1 C2);
      set lib.file_&next_file.;
      if C1 = &next_file.;
   run;

   proc export data = file&next_file.
               outfile = "c:\Export\file&year._&next_file."
               dbms = csv
               replace;
%end;
%mend;

%extract_data(2010)

今天,它停止工作,我得到的错误是第一个文件,其中我得到“错误 180-322:语句无效或它的使用顺序不正确”下划线集。真正奇怪的是,它只对第一个文件执行此操作(即上面的示例中的 24),然后其他导出似乎都会通过。我尝试了不同的 file_names 三元组,现在以前不起作用的一个起作用了(即 file_names = 12 24 27,在这种情况下,24 正常导出,而 12 不起作用)。

非常感谢您的帮助!

I've been using a code with seemingly no problem for a quite a while now, and today it stopped working and in a pretty weird way.

I have several SAS files in a folder and I want to export each of them into a CSV file, while keeping only some columns and if some condition is satisfied.

The file structure is folders called data_(year) and inside are several files called file_(region number)

%macro extract_data(year);
libname lib "c:\data_&year.";

%let file_names = 24 27 29;
%let nb_files = 3;

%do i=1 %to &nb_files.;

   %let next_file = %scan(&file_names, &i);

   data file&next_file. (keep = C1 C2);
      set lib.file_&next_file.;
      if C1 = &next_file.;
   run;

   proc export data = file&next_file.
               outfile = "c:\Export\file&year._&next_file."
               dbms = csv
               replace;
%end;
%mend;

%extract_data(2010)

Today this stopped working and the error I get is for the FIRST file where I get an "ERROR 180-322: Statement is not valid or it is used out of proper order" underlining set. The really weird thing is that it does this only for the first file (ie in the example above 24) and then the other exports seem to go through. I tried with different triplets of file_names and now the one which didn't work before does (ie file_names = 12 24 27 and in this case 24 exported normally and it was 12 that didn't work).

Thank you very much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2025-01-23 01:57:38

最可能的问题是宏变量中的不可见字符,这会导致宏生成无效的数据集名称。

尝试重新键入分配给 FILE_NAMES 宏变量的值列表,以消除任何奇怪的字符。

或者,要调试,请尝试使用如下数据步骤查看该值的十六进制代码表示形式:

 data _null_;
   string="&file_names";
   put string $hex.;
 run;

空格应显示为十六进制代码 20。数字是十六进制代码 3039。任何其他代码,如 090A0DA0 都是不可见字符。

The most likely issue is invisible characters in your macro variable that is leading to invalid dataset names being generated by your macro.

Try retyping the list of values being assigned to FILE_NAMES macro variable to eliminate any strange characters.

Or to debug try looking at the hex code representation of the value by using a data step like:

 data _null_;
   string="&file_names";
   put string $hex.;
 run;

Spaces should show as hex code 20. Digits are the hex code 30 to 39. Any other codes like 09,0A,0D,A0 are invisible characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文