在 SAS 电子指南中格式化 &宏变量日期



以下代码的要点是将上一周周日的日期和上周星期六的日期分配给宏变量。我需要 date9。格式中的它们。

在第48行的数据步骤中,我将其格式化为 date9。,但是当在数据步骤之外调用时,他们正在丢失该格式。他们只是显示为默认的SAS日期格式。

我在这里想念什么?

44         data _null_;
45             curDate= today();
46             sunday = intnx('week1.1',curDate,-1,'Begin');
47             saturday = intnx('week1.1',curDate,-1,'End');
48             format _all_ date9.;
49             put (_all_)(=/);
50          call symput('sunday',sunday);
51          call symput('saturday',saturday);
52          
53         run;
NOTE: Numeric values have been converted to character values at the places given     by: (Line):(Column).
      50:23   51:25   
curDate=23JUN2016
sunday=12JUN2016
saturday=18JUN2016
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

54          %PUT &sunday;
20617

您可以使用put直接应用格式。您可能会发现,在将来的查询中使用实际日期值的实际日期值更容易,如果您使用date9格式化,请确保在解决变量时引用并使用D。

我还建议使用Call Symputx。

call symputx('sunday',put(sunday, date9.));

这是一种常见的情况,可能会变得非常烦人...我写了一个宏观例程,允许使用我们选择的SAS格式将格式化(或未格式化)的日期分配给宏变量。

使用%letdate宏,您只是喜欢这样做:

%letdate(sunday, intnx('week1.1',today(),-1,'Begin'), date9.)
%letdate(saturday, intnx('week1.1',today(),-1,'End'), date9.)

日志显示:

*** Variable macro sunday = 12JUN2016 *** 
*** Variable macro saturday = 18JUN2016 ***

可以在此处找到宏的源代码;它是用法语记录的,但应该足够简单。只需注意:

  1. 参数1是宏变量的名称
  2. 参数2是值(正如您将在数据步骤中编写的那样)
  3. 参数3是要应用于参数2的值的SAS格式。

Reeza答案的替代方法(不同的代码),您可以将PUT函数包装在%sysfunc中,以您想要的格式输出宏日期变量;

%put %sysfunc(PutN(&sunday, date9));

结果

最新更新