BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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;

 

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

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;
Ksharp
Super User
%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;
Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1087 views
  • 2 likes
  • 4 in conversation