SAS 中的数值宏

Numeric Macro in SAS

我有一个计算天数的变量。我正在尝试使用天数除以总天数。

如何创建一个宏来存储最近一天并允许我稍后引用它?

这是我目前所拥有的(我删除了不相关的代码)


DATA scotland;
input day deathsscotland casesscotland;
cards;
1 1 85 
2 1 121 
3 1 153 
4 1 171 
5 2 195 
6 3 227  
7 6 266 
8 6 322
9 7 373 
10 10 416
11 14 499
12 16 584 
13 22 719
14 25 894
;
run;

proc sort data=scotland out=scotlandsort;
by day;
run;


Data _null_;
keep day;
set scotlandsort end=eof;
if eof then output;
run;

%let daycountscot = day

Data ratio;
set cdratio;
SCOTLANDAVERAGE = (SCOTLANDRATIO/&daycountscot)*1000;
run;

使用您自己的代码,您可以像这样创建宏变量

Data _null_;
keep day;
set scotlandsort end=eof;
if eof then call symputx('daycountscot', day);
run;

%put &daycountscot.;

data _null_ 没有做任何事情。您可以通过直接将最大日期值选择到宏变量中来消除 sortdata 步骤。

proc sql noprint;
  select max(day) into :daycountscot trimmed
  from scotland
  ;
quit;

不需要为此使用宏代码,最好还是将值保存在变量中。要将值转换为文本以将其存储为宏变量,SAS 必须对数字进行舍入。

您可以创建一个具有最大DAY值的数据集,然后将其与您要进行除法的数据集合并。

data last_day;
  set scotlandsort end=eof;
  if eof then output;
  keep day;
  rename day=last_day;
run;

data ratio;
  set cdratio;
  if _n_=1 then set last_day;
  SCOTLANDAVERAGE = (SCOTLANDRATIO/last_day)*1000;
run;

在 SQL 代码中可能更容易:

proc sql;
create table ratio as 
  select a.*, (SCOTLANDRATIO/last_day)*1000 as SCOTLANDAVERAGE 
  from cdratio a
     , (select max(day) as last_day from scotland)
;
quit;