DATA Step, Macro, Functions and more

DO loop with date variable as by-variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

DO loop with date variable as by-variable

Hi all,

 

I have a problem with implementing a DO loop using date variables as by-variable.

I have searched this community and found a way. But there comes an error when I tweak a little.

See the code first. This one works fine.

 

 

options symbolgen;
%macro daily00(X) /des = "Sample Macro";
	%do i=1 %to 3;
		%put &X._&i;
	%end;
%mend daily00;
%macro iteration00(BeginDate, EndDate) /des = "Sample Macro";
		%do j=%sysfunc(inputn(&begindate,date9.)) %to %sysfunc(inputn(&enddate,date9.));
			%daily00(X=&j);
		%end;
%mend iteration00;
%iteration00(BeginDate=23Dec2014, EndDate=24Dec2014);

Now I want to change the BeginDate and EndDate to YYYYMMDD, so in this case it would be 20141223 and 20141224, respectively.

I tried the below but something's wrong. I think I am confused with INPUT or INFORMAT syntax, but I am lost.

 

options symbolgen;
%macro daily00(X) /des = "Sample Macro";
	%do i=1 %to 3;
		%put &X._&i;
	%end;
%mend daily00;
%macro iteration00(BeginDate, EndDate) /des = "Sample Macro";
		%do j=%sysfunc(inputn(&begindate,8.)) %to %sysfunc(inputn(&enddate,8.));
			%daily00(X=&j);
		%end;
%mend iteration00;
%iteration00(BeginDate=20141223, EndDate=20141224);

 

The reason I am trying to use date format in this DO loop is because, I want to avoid numeric values such as 20141240 when calling datasets in the DAILY00 macro.

 

I am using SAS 9.4.

Any help is appreciated.


Accepted Solutions
Solution
‎01-16-2018 07:15 PM
Super User
Posts: 6,933

Re: DO loop with date variable as by-variable

Yes, the informat is incorrect.  You are using:

 

8.

 

But should be using:

 

yymmdd8.

 

Also note, you define %daily00 using X as a positional parameter.  But your macro call uses it as a keyword parameter.  Presumably that's just a typo.

View solution in original post


All Replies
Solution
‎01-16-2018 07:15 PM
Super User
Posts: 6,933

Re: DO loop with date variable as by-variable

Yes, the informat is incorrect.  You are using:

 

8.

 

But should be using:

 

yymmdd8.

 

Also note, you define %daily00 using X as a positional parameter.  But your macro call uses it as a keyword parameter.  Presumably that's just a typo.

Occasional Contributor
Posts: 11

Re: DO loop with date variable as by-variable

Posted in reply to Astounding

Thanks you. It works!

I was keep trying

 

yymmddn8.

 

since the numbers didn't have any usual delimiters such as / or -.

 

Anyway, thank you so much!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 268 views
  • 1 like
  • 2 in conversation