对多个数据集使用相同条件

问题描述

我的代码如下:

%macro xx(date);
proc sql;
create table test_&date as
select a.*
from population_&date as a
left join new_acct_no as b on a.names = b.names and put(intnx('month',input(put(&date,8. -l),yymmn6.),'e'),yymmdd10.) between b.birthdate and b.marriage_dt
left join old_acct_no as c on a.names = c.names and put(intnx('month',yymmdd10.) between c.birthdate and c.marriage_dt
; 
quit;
%mend;
%date(201812);

我想通过替换“ put(intnx('month',yymmdd10.)”来使代码更简洁。

任何想法如何做到这一点?

解决方法

将静态计算的结果放在Proc SQL之前的新符号中。 另外,调用它时,请使用正确的宏名称。您定义了xx,并且正在调用date

示例:

%macro Population_For(date);

%local this_month next_month;

%let this_month = %sysfunc(inputn(&date,yymmn6.));
%let next_month = %sysfunc(intnx(month,&this_month,1));

%put NOTE: SAS Date values &=this_month &=next_month;

proc sql check;
create table test_&date as
select a.*
from population_&date as a
left join new_acct_no as b on a.names = b.names and &next_month between b.birthdate and b.marriage_dt
left join old_acct_no as c on a.names = c.names and &next_month between c.birthdate and c.marriage_dt
; 
quit;
%mend;

%Population_For(201812);

正确的编码还取决于日期值在表new_acct_noold_acct_no中的存储方式。他们是

  • SAS日期值
  • 构造物 yyyymmdd 的数值(即编码年* 10000 +月* 100 +天)
  • 构造 yyyymmdd yyyy-mm-dd
  • 的字符串

注意:由SAS格式yyyy-mm-dd呈现的日期表示形式 yymmdd10. other 数据库系统查询中解释为日期文字,在SAS中不是这样。

日期数据存储为构造体 yyyy-mm-dd

的字符串

经过正确审查的YMD字符串的词典顺序与它们表示的SAS日期值的顺序相同。

因此,如果两个表都将日期存储为YMD字符串,则可以通过计算并使用静态next_month ymd表示来处理这样的日期数据 。这样的编码将消除在查询期间将字段birthdatemarriage_dt中的日期数据转换为SAS日期的要求。

%local this_month next_month next_YMD;

%let this_month = %sysfunc(inputn(&date,1));
%let next_YMD   = %sysfunc(putn(&next_month,yymmdd10.));

%put NOTE: SAS Date values &=this_month &=next_month;

proc sql check;
create table test_&date as
select a.*
from population_&date as a
left join new_acct_no as b on a.names = b.names and &next_month between b.birthdate and b.marriage_dt
left join old_acct_no as c on a.names = c.names and &next_month between c.birthdate and c.marriage_dt
;