我想知道我可以将一些代码合并到一个PROC SQL
语句中,而不是几个背靠背的SQL
语句。
我正在使用宏来计算参与者在研究同意日期的年龄,类似于此处和此处提供的内容。我尝试对宏的功能进行故障排除,并使用%sum宏计算了婴儿出生时的总重量(以盎司为单位)(效果很好……)。但是,在尝试计算年龄时,宏不起作用。
但是,如果我在新SQL
语句中使用宏,它就可以正常工作。
下面的代码有效:
%macro months(somedate,birth);
intck('month',&birth,&somedate)
- (day(&somedate) < day(&birth))
%mend months;
%macro days(somedate,birth);
intck('day',&birth,&somedate)
- (day(&somedate) < day(&birth))
%mend days;
%macro sum(part1, part2);
&part1*16 + &part2
%mend sum;
********** bringing in data from outside tables ;
proc sql;
create table demos as
select x.*, infcondt2 as c_dt,
y.*, datepart(visitdt) as v_dt format date9. ,
datepart(birthdt) as b_dt format date9. ,
birthweightlbs as lbs,
birthweightoz as oz,
lbs*16 + oz as tot_oz,
%sum(lbs,oz) as tot_oz_m
from enrolled as x,
demographics as y
where x.center = y.center and x.id = y.id ;
quit;
********** calculating age in months and in days ;
proc sql;
create table demos2 as
select * ,
%months(c_dt, b_dt) as age_m ,
%days(c_dt, b_dt) as age_d
from demos;
quit;
********** creating age groupings by months: 0-3 and 3-6 ;
proc sql;
create table demos3 as
select * ,
case
when age_m le 3 then 1
when age_m le 6 and age_m gt 3 then 2
else 3
end as age_interval
from demos2;
quit;
有没有办法把它合并成一个语句?就像是:
proc sql;
create table demos as
select x.*, infcondt2 as c_dt,
y.*, datepart(visitdt) as v_dt format date9. ,
datepart(birthdt) as b_dt format date9. ,
birthweightlbs as lbs,
birthweightoz as oz,
lbs*16 + oz as tot_oz,
%sum(lbs,oz) as tot_oz_m,
%months(c_dt, b_dt) as age_m,
%days(c_dt, b_dt) as age_d,
case
when age_m le 3 then 1
when age_m le 6 and age_m gt 3 then 2
else 3
end as age_interval
from enrolled as x,
demographics as y
where x.center = y.center and x.id = y.id ;
quit;