如何解决对多个数据集使用相同条件
我的代码如下:
%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_no
和old_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表示来处理这样的日期数据 。这样的编码将消除在查询期间将字段birthdate
和marriage_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
;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。