从输入文件中提取数年的时间间隔[SAS]
我正在使用SAS宏来运行多个月度文件,并提取进一步分析所需的变量。当前,该程序将每个月度文件获取,提取所需的内容并将其作为同一每月文件输出。我还将其设置为将所有内容组合到年度文件中。在大多数情况下,这正常工作:
%let start_date = '31jan2022'd;
%let end_date = '31mar2022'd;
%let num_years = %sysfunc(intck(year,&start_date,&end_date));
data _null_;
call symput('start_loop',compress(intck('month',&start_date,date())*-1));
call symput('end_loop',compress(intck('month',&end_date,date())*-1));
%MACRO MONTH_EXTRACT;
%do l=&start_loop. %to &end_loop.;
data _null_;
call symput ('monyy',put(intnx('month',date(),&l.,'end'),monyy5.));
call symput ('end_mon',put(intnx('month',date(),&l.,'end'),date9.));
call symput ('date',put(intnx('month',date(),&l.,'end'),yymmn.));
run;
&let file=libref.monthly_&date.;
%let file_year=%substr(&date,1,4);
data file_&monyy. (keep=var1 var2 var3);
set &file;
inpt_dt=&date;
proc append base=files_&file_year force data=file_&monyy.;
%end;
%mend;
当开始日期和结束日期在同一年内包含时,这正常工作。但是,所需的是,当开始日期和结束日期不在同年之内时,每年将在间隔内汇编每年的文件。前任。
start_date = '31oct2021'd;
end_date = '31mar2022'd;
这将生成两个称为Files_2021和Files_2022的输出。当我使用当前代码运行它时,它仅生成2021的第一个文件。
我尝试添加:
%IF &num_years > 1 %then %do;
%LET start_year=input(substr(&start_date,9,-4),4.0);
start_month=input(&start_date,mmddyy10.);
%LET start_month=substr(start_month,1,2);
%LET end_year=input(substr(%end_date,9,-4),4.0);
end_month=input(&end_date,mmddyy10.);
%LET end_month=substr(end_month,1,2);
%DO file_interval=&start_year %to &end_year;
months=0;
%if file_interval=&start_year %then %do
mstart=&start_month;
%end;
%else %do
mstart=1;
%end;
%if file_interval=&end_year %then %do
mstop=&end_month;
%end;
%else %do
mstop = 12;
%end;
months=mstop-mstart+1;
%end;
我知道需要一个PROC附录,但是我不知道该数据是什么要附加的实例。我也知道现在我只在数月数。我如何每年隔离并从每月创建年度输出文件?
I'm using SAS macros to run through multiple monthly files and extract variables needed for further analysis. Currently the program takes each monthly file, extracts what's needed and outputs it as the same monthly file. I also have it set to combine everything into a yearly file. This works fine, for the most part:
%let start_date = '31jan2022'd;
%let end_date = '31mar2022'd;
%let num_years = %sysfunc(intck(year,&start_date,&end_date));
data _null_;
call symput('start_loop',compress(intck('month',&start_date,date())*-1));
call symput('end_loop',compress(intck('month',&end_date,date())*-1));
%MACRO MONTH_EXTRACT;
%do l=&start_loop. %to &end_loop.;
data _null_;
call symput ('monyy',put(intnx('month',date(),&l.,'end'),monyy5.));
call symput ('end_mon',put(intnx('month',date(),&l.,'end'),date9.));
call symput ('date',put(intnx('month',date(),&l.,'end'),yymmn.));
run;
&let file=libref.monthly_&date.;
%let file_year=%substr(&date,1,4);
data file_&monyy. (keep=var1 var2 var3);
set &file;
inpt_dt=&date;
proc append base=files_&file_year force data=file_&monyy.;
%end;
%mend;
This works fine when the start date and the end date are contained within the same year. However, what is desired is that, when the start date and end date are not within the same year, a yearly file will be compiled for each year within the interval. Ex.
start_date = '31oct2021'd;
end_date = '31mar2022'd;
This would generate two outputs called files_2021 and files_2022. When I run it with the current code, it only generates the first file for 2021.
I've attempted to add in:
%IF &num_years > 1 %then %do;
%LET start_year=input(substr(&start_date,9,-4),4.0);
start_month=input(&start_date,mmddyy10.);
%LET start_month=substr(start_month,1,2);
%LET end_year=input(substr(%end_date,9,-4),4.0);
end_month=input(&end_date,mmddyy10.);
%LET end_month=substr(end_month,1,2);
%DO file_interval=&start_year %to &end_year;
months=0;
%if file_interval=&start_year %then %do
mstart=&start_month;
%end;
%else %do
mstart=1;
%end;
%if file_interval=&end_year %then %do
mstop=&end_month;
%end;
%else %do
mstop = 12;
%end;
months=mstop-mstart+1;
%end;
I know that a proc append is needed, but I don't know what would be the data to append in this instance. I also know that right now I'm only counting the months. How can I isolate each year and create yearly output files from the monthly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其按年循环并计算本月,不如按月循环并计算年度。您可以通过引用开始日期来增加循环。例如:
开始日期= 01JAN2020
末期日期= 01MAR2020
我们可以使用
intck()
and code> and and and and 和<代码> intnx()进行计算。输出:
Instead of looping by year and calculating the month, loop by month and calculate the year. You can increment your loop by referencing off of the start date. For example:
Start Date = 01JAN2020
End Date = 01MAR2020
We can use a combination of
intck()
andintnx()
to calculate this.Output: