Hello
I have the following task.
User define year+month (for example: 1907 is July 2019).
Then need to create a data set that contain all dates between start of 1907 and end of 1907.
The dates in table should be char type with following structure YYMMDD.
So I need to create following values:
190701
190702
190703
190704
190705
.........
.........
190731
I run this program and it run long time with no finish.
What is the problem here please?
%let CurMon=1907;
data _null_;
date_start=mdy(mod(&CurMon,100),1,floor(&CurMon/100));
date_end=intnx ('month',date_start,0,'E');
counter = intck('month',date_start,date_end);
/*call symputx('date_start',put(date_start,best.));*/
/*call symputx('date_end',put(date_end,best.));*/
call symputx('date_start',"'"||put(date_start,date9.)||"'d");
call symputx('date_end',"'"||put(date_end,date9.)||"'d");
call symputx('n',put(counter,best.));
run;
%put &date_start.;
%put &date_end.;
data want_days;
date="&date_start";
do while (date<="&date_end");
output;
date=intnx('day', date, 1, 's');
end;
format date date9.;
run;
Hi @Ronein
You can try this code, which is simpler:
%let CurMon=1902;
data want_days;
date_start_n = mdy(input(substr(strip(&CurMon),3,2),2.),1,input(substr(strip(&CurMon),1,2),2.));
date_end_n = intnx('month',date_start_n,0,'e');
do date_n = date_start_n to date_end_n;
date = strip(put(date_n,YYMMDD6.));
output;
end;
drop date_start_n date_end_n date_n;
run;
%let CurMon=1907;
data want;
temp=input("&CurMon.01",yymmdd6.);
do d=temp to intnx('month',temp,0,'e');
date=put(d,yymmdd6.);output;
end;
keep date;
run;
Your habit of using formatted date values causes you to stumble over your own feet.
This
date="&date_start";
resolves to this
date="'01JUL2019'd";
which is not a valid SAS date literal.
Simplify your code:
%let CurMon=1907;
data _null_;
date_start=mdy(mod(&CurMon,100),1,floor(&CurMon/100));
date_end=intnx ('month',date_start,0,'E');
counter = intck('month',date_start,date_end);
call symputx('date_start',date_start);
call symputx('date_end',date_end);
call symputx('n',put(counter,best.));
run;
%put &date_start.;
%put &date_end.;
data want_days;
do date = &date_start. to &date_end.;
output;
end;
format date date9.;
run;
and take the time to study and internalize Maxim 28.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.