As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Macro Loops with Dates

From sasCommunity
Jump to: navigation, search

Writing Macro Loops with Dates from Then to Now

Overview

Programs

Program Info-from-Dates-from-Mvars

 /*    name: Info-from-dates-from-mvars
description: provide list of system mvars with dates
purpose    : show how to convert dates to integers
sas.help: About SAS Date, Time, and Datetime Values
          Dictionary of Functions and CALL Routines
          Working with Dates in the SAS System: Comparing Durations and SAS Date Values
          Working with Dates in the SAS System: Understanding How SAS Handles Dates
other formats with year2:
%put today: &today %sysfunc(putn(&today,date7.));
%put today: &today %sysfunc(putn(&today,mmddyy10.));
*******/
%put sysdate: &Sysdate.;
%put sysdate9: &Sysdate9.;
 
%let today = %sysevalf('10JAN2013'd);
%let today = %sysevalf('10Feb2013'd);
%let today = %sysevalf('21Dec2012'd);
*let today = %sysevalf("&Sysdate9."d);
*let today = %sysfunc(today()) ;
*let today = %sysfunc(date()) ;
 
%put today: &today %sysfunc(putn(&today,date9.));
%put today: &today %sysfunc(putn(&today,mmddyy10.));
%put today: &today %sysfunc(putn(&today,worddate18.));
%put today: &today %sysfunc(putn(&today,weekdate29.));
*weekdate29 is the datetime-stamp in title1, update by setting option dtreset;
 
*parts:;
%put day/month: &today %sysfunc(day(&today));
%put month: &today %sysfunc(month(&today));
%put year: &today %sysfunc(year(&today));
 
%put week/year: &today %sysfunc(week(&today,u));
%put week/year: &today %sysfunc(week(&today,v));
%put week/year: &today %sysfunc(week(&today,w));
 
%put day/year: &today %substr(%sysfunc(juldate7(&today)),5,3);

log:

14         %put sysdate9: &Sysdate9.;
sysdate9: 15JAN2013
15         
18         %let today = %sysevalf('21Dec2012'd);

23         %put today: &today %sysfunc(putn(&today,date9.));
today: 19348 21DEC2012
24         %put today: &today %sysfunc(putn(&today,mmddyy10.));
today: 19348 12/21/2012
25         %put today: &today %sysfunc(putn(&today,worddate18.));
today: 19348  December 21, 2012
26         %put today: &today %sysfunc(putn(&today,weekdate29.));
today: 19348     Friday, December 21, 2012
27         
28         *parts:;
29         %put day/month: &today %sysfunc(day(&today));
day/month: 19348 21
30         %put month: &today %sysfunc(month(&today));
month: 19348 12
31         %put year: &today %sysfunc(year(&today));
year: 19348 2012
32         
33         %put week/year: &today %sysfunc(week(&today,u));
week/year: 19348 51
34         %put week/year: &today %sysfunc(week(&today,v));
week/year: 19348 51
35         %put week/year: &today %sysfunc(week(&today,w));
week/year: 19348 51
36         
37         %put day/year: &today %substr(%sysfunc(juldate7(&today)),5,3);
day/year: 19348 356

Macro Do-Dates

This macro has parameters for the macro do loop: date-begin, -end and increment, and the date interval for the intnx function.

 /*    name: do_dates
   location: ...\SAS-site\macros
     author: Ronald J. Fehd  2013
description: macro do loop from date-begin to date-end
purpose    : standardize calling of other macros for
sas.help   : Incrementing Dates and Times
              by Using Multipliers and by Shifting Intervals
sas.wiki   : http://www.sascommunity.org/wiki/Macro_Loops_with_Dates
predecessor: http://www.sascommunity.org/wiki/Macro_CallMacr
******/
%Macro do_dates
         (date_begin = /* integer or date literal */
         ,date_end   = /* integer or date literal */
         ,increment  =1   /* greater than or equal to interval:
                             week:7 month:31 quarter:91 year:366 */
         ,interval   =day /* week month quarter etc */
         ,MacroName  =put note
         ,MacroParms = /*%nrstr(data=sashelp.class,var=sex)*/
         ,semicolon  =0
         ,testing    =0
         );
%let Testing = %eval(&Testing
                     or %sysfunc(getoption(mprint)) eq MPRINT);
%if %scan(&MacroName,1) eq put %then %do;
    %let Semicolon = 1;
    %let Testing   = 1;
    %end;
%local MacroCall ThisDay;
 
%do ThisDay = &Date_Begin %to &Date_End %by &Increment;
    %if &Testing %then %put
        note: ThisDay: &ThisDay %sysfunc(putn(&ThisDay,weekdate29.));
    %let MacroCall = &MacroName(;
    %if %length(&MacroParms) %then
        %let MacroCall = &MacroCall.%unquote(&MacroParms,);
    %let MacroCall =
     &MacroCall.Date_begin=%sysfunc(intnx(&Interval,&ThisDay,0,begin));
    %let MacroCall =
      &MacroCall.,Date_end=%sysfunc(intnx(&Interval,&ThisDay,0,end)));
    %&MacroCall.
    %if &Semicolon %then %do;
        ;
        %end;
    %end;
%mend do_dates;

Program Do-Dates-tests

options mprint;
%do_dates(date_begin =0
         ,date_end   =7
         ,interval   =day
         ,increment  =1
         );
%do_dates(date_begin =0
         ,date_end   =92
         ,interval   =month
         ,increment  =31
         );
%do_dates(date_begin =0
         ,date_end   =366
         ,interval   =quarter
         ,increment  =92
         );
%do_dates(date_begin =0
         ,date_end   =366
         ,interval   =month
         ,increment  =31
         );

log:

2          %do_dates(date_begin =0
3                   ,date_end   =7
4                   ,interval   =day
5                   ,increment  =1
6                   );
note: ThisDay: 0       Friday, January 1, 1960
note(Date_begin=0,Date_end=0)
note: ThisDay: 1     Saturday, January 2, 1960
note(Date_begin=1,Date_end=1)
note: ThisDay: 2       Sunday, January 3, 1960
note(Date_begin=2,Date_end=2)
note: ThisDay: 3       Monday, January 4, 1960
note(Date_begin=3,Date_end=3)
note: ThisDay: 4      Tuesday, January 5, 1960
note(Date_begin=4,Date_end=4)
note: ThisDay: 5    Wednesday, January 6, 1960
note(Date_begin=5,Date_end=5)
note: ThisDay: 6     Thursday, January 7, 1960
note(Date_begin=6,Date_end=6)
note: ThisDay: 7       Friday, January 8, 1960
note(Date_begin=7,Date_end=7)

References

--Ronald_J._Fehd macro.maven == the radical programmer 18:43, 15 January 2013 (EST)