SAS 重命名带有开放括号的文件

huangapple go评论55阅读模式
英文:

SAS Rename of file with open parentheses

问题

我有大量的Excel文件需要处理。我已经创建了代码,将它们的名称读入数据集以执行每个文件。不幸的是,每个文件都有一个打开的左括号,例如:"(",例如:Jason (Allen.xlsx

我已经编写了代码来清理我的清单数据集中的名称,但似乎无法使用带有打开括号的RENAME宏。使用两个虚拟的Excel文件,school_file_list数据集中的第一个记录将更改名称,因为它有关闭的括号...但第二个文件名不会更改。是否有一种方法可以编辑我的宏以将具有问题的打开括号的文件名传递给它?

data school_file_list;
    input fname $ 1-18 newfname $ 20-42;
    cards;
    Jason (Allen).xlsx Jason Allen.xlsx
    Jason (A.xlsx      Jason A.xlsx
    ;
run;

%let filepath= %nrbquote(C:\mypath\);

%macro file_rename(from, to);
data _null_;
    rc=rename("&filepath.&from", "&filepath.&to", "file");
    if rc ne 0 then do;
        put "Rename of file &from to &to DID NOT WORK!!!" rc=;
        call symput('err','1');
    end;
    else put "Rename of file &from to &to WORKED.";
run;
%mend file_rename;

data _null_; set school_file_list;
    call execute ('%file_rename(from='||strip(fname)||', to='||strip(newfname)||');');
run;

Note: 以上是您提供的代码的翻译部分,不包括您要求不翻译的代码部分。

英文:

I have a large number of Excel files to be processed. I've created code that reads their names into a dataset for execution of each file. Each of the files, unfortunately, has an open left parentheses like "(", such as: Jason (Allen.xlsx

I've written code to clean up the names in my listing data set, but I can't seem to get the RENAME based macro to work with the open parentheses. Using two dummy Excel files, the first record in the school_file_list dataset will change names just fine because it has closed parentheses...but the second file name will not change. Is there a way to edit my macro to feed it the file names that have the offending open parentheses?

data school_file_list;
	input fname $ 1-18 newfname $ 20-42;
	cards;
Jason (Allen).xlsx Jason Allen.xlsx
Jason (A.xlsx      Jason A.xlsx
;
run; 

%let filepath= %nrbquote(C:\mypath\);

%macro file_rename(from, to);
data _null_;
	rc=rename("&filepath.&from", "&filepath.&to", "file");
	if rc ne 0 then do;
		put "Rename of file &from to &to DID NOT WORK!!!" rc=;
		call symput('err','1');
	end;
	else put "Rename of file &from to &to WORKED.";
	run;
%mend file_rename;

data _null_; set school_file_list;
	call execute ('%file_rename(from='||strip(fname)||', to='||strip(newfname)||');');
	run;

答案1

得分: 3

以下是翻译好的部分:

"问题似乎是宏而不是名称或重命名。不确定宏在此过程中添加了什么价值。为什么不直接在数据步骤中发布重命名语句呢?

data _null_;
  set school_file_list;
  rc=rename(cats("&filepath",fname),cats("&filepath",newfname),"file");
...

如果需要创建一个宏,那就创建一个以数据集名称作为输入的宏。

类似这样:

%macro file_rename(dsname,from=from,to=to,filepath=" ");
data _null_;
  set &dsname;
  rc=rename(cats(&filepath,&from),cats(&filepath,&to),"file");
  if rc ne 0 then do;
     put "Rename of file " &from "to " &to "in " &filepath "DID NOT WORK!!!" rc=;
     call symput('err','1');
  end;
  else put "Rename of file " &from "to " &to "in " &filepath "WORKED.";
run;
%mend file_rename;

对于您的示例数据集和根路径,您可以这样调用它:

%file_rename
(dsname=school_file_list
,from=fname
,to=newfname
,filepath="&filepath"
);

这将生成如下代码:

%let filepath=c:\;
%file_rename(dsname=school_file_list,from=fname,to=newfname,filepath="&filepath");
MPRINT(FILE_RENAME):   data _null_;
MPRINT(FILE_RENAME):   set school_file_list;
MPRINT(FILE_RENAME):   rc=rename(cats("c:\",fname),cats("c:\",newfname),"file");
MPRINT(FILE_RENAME):   if rc ne 0 then do;
MPRINT(FILE_RENAME):   put "Rename of file " fname "to " newfname "in " "c:\" "DID NOT WORK!!!" rc=;
MPRINT(FILE_RENAME):   call symput('err','1');
MPRINT(FILE_RENAME):   end;
MPRINT(FILE_RENAME):   else put "Rename of file " fname "to " newfname "in " "c:\" "WORKED.";
MPRINT(FILE_RENAME):   run;

通过进行以下微小更改:

%if %length(&dsname) %then %do;
  set &dsname;
%end;

您还可以使宏在没有数据集的情况下运行。

%file_rename
(from='Jason (Allen).xlsx'
,to='Jason Allen.xlsx'
,filepath="&filepath"
);
英文:

It seems to me the issue is the macro and not the names or the renaming. Not sure what value the macro is adding in this process. Why not just issue the rename statements directly in the data step?

data _null_;
  set school_file_list;
  rc=rename(cats("&filepath",fname),cats("&filepath",newfname),"file");
...

If you need to make a macro then make one that takes the name of the dataset as the input.

Something like this:

%macro file_rename(dsname,from=from,to=to,filepath=" ");
data _null_;
  set &dsname;
  rc=rename(cats(&filepath,&from),cats(&filepath,&to),"file");
  if rc ne 0 then do;
     put "Rename of file " &from "to " &to "in " &filepath "DID NOT WORK!!!" rc=;
     call symput('err','1');
  end;
  else put "Rename of file " &from "to " &to "in " &filepath "WORKED.";
run;
%mend file_rename;

Which for your example dataset and root path you could call like this:

%file_rename
(dsname=school_file_list
,from=fname
,to=newfname
,filepath="&filepath"
);

Which will generate code like:

894  %let filepath=c:\;
895  %file_rename(dsname=school_file_list,from=fname,to=newfname,filepath="&filepath");
MPRINT(FILE_RENAME):   data _null_;
MPRINT(FILE_RENAME):   set school_file_list;
MPRINT(FILE_RENAME):   rc=rename(cats("c:\",fname),cats("c:\",newfname),"file");
MPRINT(FILE_RENAME):   if rc ne 0 then do;
MPRINT(FILE_RENAME):   put "Rename of file " fname "to " newfname "in " "c:\" "DID NOT WORK!!!" rc=;
MPRINT(FILE_RENAME):   call symput('err','1');
MPRINT(FILE_RENAME):   end;
MPRINT(FILE_RENAME):   else put "Rename of file " fname "to " newfname "in " "c:\" "WORKED.";
MPRINT(FILE_RENAME):   run;

With a minor change

%if %length(&dsname) %then %do;
  set &dsname;
%end; 

you could also make the macro work without a dataset.

%file_rename
(from='Jason (Allen).xlsx'
,to='Jason Allen.xlsx'
,filepath="&filepath"
);

huangapple
  • 本文由 发表于 2023年8月4日 01:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定