在变量生成 SAS 中使用 for 循环索引

Using for loop indices in variable generation SAS

我想在 SAS 中设置一个 for 循环,我想在其中创建时间相关表。

这个想法很简单。我有多个表,我想加入它们,我想每个月都进行此操作。我在 SAS 编码方面没有太多经验,因此非常感谢您的帮助。我的想法如下:

for i in [201401:201503]
proc sql;
create table try_i ## for each month try_201401, try_201402 etc...
select t1.var1 as var1_i ##again similar to above var1_201401, var_201402 etc..
from t1 
left join table2 t2 on condition1 ## this is not dependennt so there is index is required   
...
where t1.var'i' is not missinig ## in table t1, I have a specific variable which specifies the time period ex: var201405

提前致谢 图兰

一个简单的宏就可以了:

%macro monthly_table( year, month);
   proc sql;
   create table as try_&year.&month. as
   select t1.var1 as var&year.&month. from t1
   left join .... on...
   where t1.var&year.&month. is not missing;
   quit;
%mend monthlytable;

%macro reporting( year,month);
  %do month =1 to &month;
      %monthly_table( &year, &month)
  end;
%mend reporting;

如果您不熟悉宏:

宏月_table: %macro % mend 用于定义宏,&year &month 将 return 您在第一行括号之间提供的值。

例如%monthlyRep ( 2004, 01) 会 运行 proc sql 语句基于年值 = 2004 和月值 = 01.

宏报告: 循环一年中的几个月。 对于 2015 年,您只需在括号内指定,例如

%report( 2015, 01) = 201501