Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » How to Loop through Dates in SAS?

How to Loop through Dates in SAS?

BySubhro Posted onAugust 28, 2022February 4, 2023 Last Updated:February 4, 2023
0 Comments

In this example, you’ll see how to use a SAS macro and a Do loop in the Data step to loop through…

How to Loop through Dates in SAS?

In this example, you’ll see how to use a SAS macro and a Do loop in the Data step to loop through dates in SAS.

Using the INTNX() function, we can loop over dates based on an offset value. INTCK() can be used to determine the number of months to generate

Using the Data step to loop through dates

The INTNX() function is used to loop through dates based on an offset. For example, the INTCK() can be used to determine how many months to generate.

%let start_dt = '01jul2022'd;
%let stop_dt = '01dec2022'd;

data datelist;
diff=intck('month',&start_dt,&stop_dt);
	do i= 0 to diff;
	newdt=intnx('month',&start_dt,i,'b');
		output;
	end;
	format newdt date9.;
	drop i diff ;
run;
Loop through Dates in SAS

With the %LET statement, you can create a macro variable named &start_dt and &stop_dt.

The INTCK function returns the months between &start_dt and &stop_dt; this value is stored in the diff variable. The MONTH function is the first argument to retrieve the month interval.

diff=intck('month',&start_dt,&stop_dt);

Do loop is used to loop through the number of months between &start_dt and stop_dt.

The INTNX function increments the &start_dt date by MONTH. The B argument indicates that the returned date or DateTime value begins at the beginning of the interval. 

	do i= 0 to diff;
	newdt=intnx('month',&start_dt,i,'b');
		output;
	end;

Loop through Dates Using a Macro %DO Loop

Using this macro, you can loop through a starting and ending date by a month.

%macro date_loop(start,end);
   %let start=%sysfunc(inputn(&start,anydtdte9.));
   %let end=%sysfunc(inputn(&end,anydtdte9.));
   %let dif=%sysfunc(intck(month,&start,&end));
     %do i=0 %to &dif;
      %let date=%sysfunc(intnx(month,&start,&i,b),date9.);
      %put &date;
     %end;
   %mend date_loop;

   %date_loop(01jul2015,01feb2016)
How to Loop through Dates in SAS?

We use the %LET statement to create the value of the macro variable named &start and %end.

%let start=%sysfunc(inputn(&start,anydtdte9.));
%let end=%sysfunc(inputn(&end,anydtdte9.));

INPUTN can be used to apply ANYDTDTE. format to any value passed to &start and &end macro variables.ANYDTDTE. format results in SAS date format for &START and &end.

Use the %let statement to create a macro variable named &dif. The INTCK function returns the months between &start and &end. The MONTH function is the first argument to retrieve the month interval.

%let dif=%sysfunc(intck(month,&start,&end));

Note that when using this function within the macro facility, quotation marks are not used around MONTH, like you would in the DATA step.

The %do statement is used to loop through the number of months (&dif) between &start and &end.

%do i=0 %to &dif;
      %let date=%sysfunc(intnx(month,&start,&i,b),date9.);
      %put &date;
     %end;

The INTNX function increments the &start date by MONTH. 

The B argument indicates that the returned date or DateTime value begins at the beginning of the interval. 

The second argument to %SYSFUNC specifies the format DATE9 for the value returned from INTNX. 

Date values are written to the log using the %PUT statement.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
How to Print Empty SAS dataset information to the output window?
NextContinue
SAS Loops – Understanding Leave and Continue

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Proc SQL Case When Statement: A Guide to Efficient Data Analysis
  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You