Loops 如何让SAS中的do循环使用日期值?

Loops 如何让SAS中的do循环使用日期值?,loops,date,if-statement,sas,Loops,Date,If Statement,Sas,我有一个包含4年数据(2014-2017)的数据集。 我想给每个观测值一个周期变量,其中包含来自if标准的E、E/F或F 我每年都在一个数据步骤中重复我的代码,从而实现了这一目标: 数据更新; 变老 format period $10.; if year=2014 then do; if start<'01feb2014'd and end<='01mar2014'd then period='E'; else if start<'01feb2014'd a

我有一个包含4年数据(2014-2017)的数据集。 我想给每个观测值一个周期变量,其中包含来自if标准的E、E/F或F

我每年都在一个数据步骤中重复我的代码,从而实现了这一目标:

数据更新; 变老

format period $10.;

if year=2014 then do;
    if start<'01feb2014'd and end<='01mar2014'd then period='E'; 
    else if start<'01feb2014'd and end>'01mar2014'd then period='E/F'; 
    else if start>='01feb2014'd then period='F';
end;
if year=2015 then do;
    if start<'01feb2015'd and end<='01mar2015'd then period='E'; 
    else if start<'01feb2015'd and end>'01mar2015'd then period='E/F'; 
    else if start>='01feb2015'd then period='F';
end;

if year=2016 then do;
    if start<'01feb2016'd and end<='01mar2016'd then period='E'; 
    else if start<'01feb2016'd and end>'01mar2016'd then period='E/F'; 
    else if start>='01feb2016'd then period='F';
end;
if year=2017 then do;
    if start<'01feb2017'd and end<='01mar2017'd then period='E'; 
    else if start<'01feb2017'd and end>'01mar2017'd then period='E/F'; 
    else if start>='01feb2017'd then period='F';
end;
format period $10.;

do i=0 to 3;
    if year=(2014+i) then do;

        if start<'01feb(2014+i)'d and end<='01mar(2014+i)'d then period='E'; 
        else if start<'01feb(2014+i)'d and end>'01mar(2014+i)'d then period='E/F'; 
        else if start>='01feb(2014+i)'d then period='F';
    end;

end;
format period $10.;

do i=2014 to 2017;
    if year=(i) then do;

        if start<'01feb(i)'d and end<='01mar(i)'d then period='E'; 
        else if start<'01feb(i)'d and end>'01mar(i)'d then period='E/F'; 
        else if start>='01feb(i)'d then period='F';
    end;

end;
格式周期$10。;
如果年份=2014年,则执行;
如果开始您可以使用
MDY(3,1,2017)
从整数创建日期。您还可以使用函数
INTNX('month',dt,12,'END')+1
将12个月添加到日期
dt
,然后到达月底,添加1到下个月初

因此,如果我没有错,下面的代码应该可以工作

format period $10.;

do i=2014 to 2017;
    if year=(i) then do;

        if start<MDY(2,1, i) and end<=MDY(3,1, i) then period='E'; 
        else if start<MDY(2,1, i) and end>MDY(3,1, i) then period='E/F'; 
        else if start>=MDY(2,1, i) then period='F';
    end;

end;
格式周期$10。;
do i=2014年至2017年;
如果年份=(i),则执行;

如果start我不明白为什么需要对该数据进行DO循环。不同的年份有不同的观测结果。您可以使用
MDY()
函数从单独的月、日和年值构建日期

data new;
  set old;
  length period $10;
  if start<mdy(2,1,year) and end<=mdy(3,1,year) then period='E'; 
  else if start<mdy(2,1,year) and end>mdy(3,1,year) then period='E/F'; 
  else if start>=mdy(2,1,year) then period='F'; 
  else period=' ';
run;
数据更新;
变老;
期限10美元;

如果开始的话,这会给我想要的-和正确的结果。非常感谢。谢谢你的帮助。我只是想生成一些代码,而不需要太多的复制。
data new;
  set old;
  length year 8 period $10;
  do year=2014 to 2017;
    if start<mdy(2,1,year) and end<=mdy(3,1,year) then period='E'; 
    else if start<mdy(2,1,year) and end>mdy(3,1,year) then period='E/F'; 
    else if start>=mdy(2,1,year) then period='F'; 
    else period=' ';
    output;
  end;
run;