Hi
I am visting to create a macro variable on a accumulating financial year period, so that each month it will automatically select a date range based on the time period as we progress through the year. ie if it is Sep12 then I want to select records with a drawn date for Jul12 to Aug12, Jul11 to Aug11
or if its Feb13 then I want to select records for Jul12 to Jan13, Jul11 to Jan12.
Thanks for your help in advance.
Here's one method:
%MACRO YEARDATES(SEED) ;
/* If no value passed, assume todays date */
%IF %STR(&SEED) eq %THEN %LET SEED = date() ;
data _null_ ;
do m = 1 to 12 ;
month_start = intnx('month',&SEED,m-13,'b') ;
month_end = intnx('month',&SEED,m-13,'e') ;
call symput(cats('MS',m),cats("'",put(month_start,date9.),"'d")) ;
call symput(cats('ME',m),cats("'",put(month_end ,date9.),"'d")) ;
end ;
run ;
options nosymbolgen nomprint ;
%DO M = 1 %TO 12 ;
%PUT %NRSTR(&MS)&M resolves to &&MS&M ;
%PUT %NRSTR(&ME)&M resolves to &&ME&M ;
%END ;
%MEND ;
/* Then call the macro like this.... (check log afterwards) */
%YEARDATES ; /* date() as seed */
%YEARDATES('07feb2013'd) ;
Then use the appropriate macro variables in your code, e.g.
data last12months ;
set mydata (where=(&MS1 <= date <= &ME12)) ;
run ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.