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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 381 views
  • 0 likes
  • 2 in conversation