BookmarkSubscribeRSS Feed
magpie
Calcite | Level 5


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. 

1 REPLY 1
chrisj75
Calcite | Level 5

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 ;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 1 reply
  • 811 views
  • 0 likes
  • 2 in conversation