【问题标题】:SAS: How to proc import with different date?SAS:如何处理不同日期的导入?
【发布时间】:2019-02-27 07:12:51
【问题描述】:

我有 10 个 csv 文件想要执行 PROC IMPORT,每次一个,具体取决于用户输入的值。例如,用户将输入 201801 以运行 2018 年 1 月的 csv 文件(PROC IMPORT)。

对于这种情况,我尝试使用子字符串 &period(它存储用户输入的 yymmn6. 值),然后从那里放入 proc 导入。我的代码如下:

   %let period=201812;
%macro convertdate();
data convertdate;
year=substr("&period",1,4);
month=substr("&period",5,2);
if month='01' then newmonth='jan';
if month='02' then newmonth='feb';
if month='03' then newmonth='mac';
if month='04' then newmonth='apr';
if month='05' then newmonth='may';
if month='06' then newmonth='jun';
if month='07' then newmonth='jul';
if month='08' then newmonth='aug';
if month='09' then newmonth='sep';
if month='10' then newmonth='oct';
if month='11' then newmonth='nov';
if month='12' then newmonth='dec';

run;

/*Assign convertdate the only distinct record into macro, then set as the data step for proc import*/
%if month='01' %then %do;
    %let newmonth=jan;
%end;



/*proc import and remaining transformation from existing script*/
proc import out=rmr_raw_source
  file="/sasdata/source/user_files/re_&newmonth.2018.xlsx"

  dbms=xlsx replace;
  sheet="re_&newmonth.2018";
  getnames=no;
  dbsaslabel=none;
run;

%mend;
%convertdate;

但是,它不起作用。我收到以下警告:

警告:明显的符号引用 NEWMONTH 未解析。

谁有更好的解决方案?

【问题讨论】:

标签: sas


【解决方案1】:

您可以尝试以下方法:

%let period = '201801';                                     /*7*/
%global month;
data test123;
   period = .                                        /*1*/

      full_date = cats(period,'01');                        /*2*/
      format converted_date DATE10.;                        /*3*/
      converted_date = input(full_date, yymmdd8.);          /*4*/
      month = put(converted_date,monname3.);                /*5*/
      call symput('month',month);                           /*6*/
  run;
  1. 从宏中创建一个 SAS 变量。
  2. 设置为给定月份的第一个日期。
  3. 创建一个新变量并设置日期格式。
  4. 将 TEXT 日期转换为 SAS 日期。
  5. 使上面创建的月份名称过期。
  6. 从 SAS 变量月中创建一个名为“月”的宏变量。
  7. 将宏变量“月”设为全局变量。

您现在可以在代码中的任何位置使用月份宏变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多