将Excel文件的最后修改日期附加到导入的表格中。

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

SAS attach excel last modified date to imported table

问题

I was trying to import excel to sas grid and add the last modified timestamp for the file into the table.

参考 sas support,我能够检索到最后修改的时间。但我在将时间添加为新列到表中时遇到问题。

我得到了这个错误:

SYMBOLGEN: Macro Variable LASTMODIFIED resolves to 09May2023:16:33:19
NOTE: LINE generated by the macro variable "LASTMODIFIED"
282   09May2023:16:33:19
      ----------
       22
       76

ERROR 22-322: Syntax error, expecting one of the following: !, !!....
ERROR 76-322: Syntax error, statement will be ignored.

以下是我尝试的代码:

%macro FileAttribs(filename);           
  %local rc fid fidc ModifyDT; 
  %let rc=%sysfunc(filename(onefile,&filename)); 
  %let fid=%sysfunc(fopen(&onefile));
     %if &fid ne 0 %then %do;      
  %let ModifyDT=%sysfunc(finfo(&fid,Last Modified));    
  %let fidc=%sysfunc(fclose(&fid));    
  %let rc=%sysfunc(filename(onefile));    
  %put NOTE- Last modified &modifydt;

  proc sql;
  create table work.final as
  select *, &modifydt. as lastmodified
  from work.tb; --从Excel导入的表

     %end;
        %else %put &filename could not be open.;
%mend FileAttribs;

也尝试将变量转换为不同的数据类型,但也失败了。

英文:

I was trying to import excel to sas grid and add the last modified timestamp for the file into the table.

Referred to the sas support I was able to retrieve the last modified time. But I am having trouble with add the time as a new column into the table.

I got this error:

SYMBOLGEN: Macro Variable LASTMODIFIED resolves to 09May2023:16:33:19
NOTE: LINE generated by the macro variable "LASTMODIFIED"
282   09May2023:16:33:19
      ----------
       22
       76

ERROR 22-322: Syntax error, expecting one of the following: !, !!....
ERROR 76-322: Syntax error, statement will be ignored.

Here is the code I tried:

%macro FileAttribs(filename);           
  %local rc fid fidc ModifyDT; 
  %let rc=%sysfunc(filename(onefile,&filename)); 
  %let fid=%sysfunc(fopen(&onefile));
     %if &fid ne 0 %then %do;      
  %let ModifyDT=%sysfunc(finfo(&fid,Last Modified));    
  %let fidc=%sysfunc(fclose(&fid));    
  %let rc=%sysfunc(filename(onefile));    
  %put NOTE- Last modified &modifydt;

  proc sql;
  create table work.final as
  select *, &modifydt. as lastmodified
  from work.tb; --the imported table from excel

     %end;
        %else %put &filename could not be open.;
%mend FileAttribs;

Also tried to cast the variable to different datatype but also failed.

答案1

得分: 1

The macro variable is a text string which confuses SQL. Add double quotes and the letters DT to tell SAS you want this to be interpreted as a datetime. Double quotes are for the macro variable to resolve. More information on date constants can be found here.

"&modifydt."dt as lastmodified format=datetime20. 
英文:

The macro variable is a text string which confuses SQL. Add double quotes and the letters DT to tell SAS you want this to be interpreted as a datetime. Double quotes are for the macro variable to resolve. More information on date constants can be found here.

"&modifydt."dt as lastmodified format=datetime20. 

答案2

得分: 0

这段代码中,有一些宏变量和SAS函数的使用。以下是已翻译的部分:

为什么要将值放入宏变量中,如果你需要在数据集中使用它的话?而不是使用 %SYSFUNC(),这样你就可以直接在数据步骤中调用 SAS 函数。

%macro FileAttribs(filename);   
%local found;
%let found=0;

filename onefile "&filename" ;
data lastmodified;
  fid = fopen('onefile');
  if fid then do;
    lastmodified = input(finfo(fid,'Last Modified'),datetime19.);
    call symputx('found','1');
    put 'NOTE: FILENAME=' "&filename" lastmodified= ;
    fid=fclose(fid);
  end;
  else put 'WARNING: FILENAME=' "&filename" ' not found.';
  format lastmodified datetime19.;
  drop fid ;
run;
filename onefile ;

%if &found %then %do;
data work.final;
  set work.tb ;
  if _n_=1 then set lastmodified;
run;
%end;
%mend FileAttribs;

请注意,我已经将代码中的 HTML 转义字符还原为正常的字符。

英文:

Why did you go to all the trouble to put the value into a macro variable if you needed it in a dataset? Instead of using %SYSFUNC() so you can call SAS functions with macro code just use a data step so you can call the functions directly.

%macro FileAttribs(filename);   
%local found;
%let found=0;

filename onefile "&filename" ;
data lastmodified;
  fid = fopen('onefile');
  if fid then do;
    lastmodified = input(finfo(fid,'Last Modified'),datetime19.);
    call symputx('found','1');
    put 'NOTE: FILENAME=' "'&filename'" lastmodified= ;
    fid=fclose(fid);
  end;
  else put 'WARNING: FILENAME=' "'&filename'" ' not found.';
  format lastmodified datetime19.;
  drop fid ;
run;
filename onefile ;

%if &found %then %do;
data work.final;
  set work.tb ;
  if _n_=1 then set lastmodified;
run;
%end;
%mend FileAttribs;

Note: This code will only work on Unix as the FINFO() function on Windows is not able to get the 'Last Modified' metadata about a file.

huangapple
  • 本文由 发表于 2023年5月11日 06:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223089.html
匿名

发表评论

匿名网友

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

确定