SAS,从数据集列创建文件名

发布于 2024-10-19 23:54:10 字数 321 浏览 1 评论 0原文

是否可以根据存储在数据集列中的值创建文件名?我所追求的是这样的:

/* 
   other code here, assume work.users looks like 
   user_id
   ImageData
 */

data _null_;
   set work.users;

   file_name=cat('/home/me/', trim(user_id), '.jpg');

   file file_name NOPRINT;

   put ImageData;
run;

目前我正在尝试用宏来做到这一点,但我没有任何运气。

is it possible to create a filename from a value stored in a dataset column? What i'm after is something like:

/* 
   other code here, assume work.users looks like 
   user_id
   ImageData
 */

data _null_;
   set work.users;

   file_name=cat('/home/me/', trim(user_id), '.jpg');

   file file_name NOPRINT;

   put ImageData;
run;

at the moment i'm trying to do it with macros but i'm not having any luck.

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

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

发布评论

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

评论(2

旧话新听 2024-10-26 23:54:10

为此,您需要首先创建 file_name 变量,然后可以在新数据步骤中使用 filevar= 选项动态写入文件。

因此,首先在 work.users 中创建 file_name:

data work.users;
  length file_name $255;
  file_name=cats('/home/me',user_id,'.jpg');
run;

然后使用 filevar= 选项执行您想要执行的操作:

data _null_;
  set work.users;
  file dummy filevar=file_name noprint;
  put ImageData;
run;

请注意,dummy 在使用 < 时只是一个占位符code>filevar= 方法。

To do this, you'll need to create the file_name variable first, and then you can use the filevar= option in a new data step to dynamically write to the files.

So, first create file_name in work.users:

data work.users;
  length file_name $255;
  file_name=cats('/home/me',user_id,'.jpg');
run;

Then do what you're trying to do using the filevar= option:

data _null_;
  set work.users;
  file dummy filevar=file_name noprint;
  put ImageData;
run;

Note that dummy is just a placeholder when using the filevar= method.

七婞 2024-10-26 23:54:10

如果您想使用宏来执行此操作:

data _null_;
  set work.users;
  call symput('filename', cats('/home/me/', user_id, '.jpg'));
run;

data _null_;
  set work.users;
  file "&filename." noprint;
  put imagedata;
run;

但是,这假设 work.users 中只有一个观察结果,我猜这不是真的。如果您想为每个观察输出一个文件,请将其放入宏中:

%macro writefile(n);
%do i = 1 %to &n;

data _null_;
    i = &i;
    set users point=i;
    call symput('filename', cats('c:\temp\', user_id, '.txt'));
    stop;
run;

data _null_;
    i = &i;
    set users point=i;
    file "&filename." noprint;
    put imagedata;
    stop;
run;

%end;
%mend;

这里,参数 &n 是数据集中的观察数量。您可以通过编程方式获取它,但对于当前目的而言,只需将其手动传递给宏会更容易。

If you want to do this with macros:

data _null_;
  set work.users;
  call symput('filename', cats('/home/me/', user_id, '.jpg'));
run;

data _null_;
  set work.users;
  file "&filename." noprint;
  put imagedata;
run;

However, this assumes there is only one observation in work.users, which I'm guessing is not true. If you want to output a file for each observation, roll it into a macro:

%macro writefile(n);
%do i = 1 %to &n;

data _null_;
    i = &i;
    set users point=i;
    call symput('filename', cats('c:\temp\', user_id, '.txt'));
    stop;
run;

data _null_;
    i = &i;
    set users point=i;
    file "&filename." noprint;
    put imagedata;
    stop;
run;

%end;
%mend;

Here, the argument &n is the number of observations in your dataset. You could obtain it programmatically, but it's easier for current purposes just to pass it manually to the macro.

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