Help using Base SAS procedures

MACRO DATE MANIPULATION - please help!

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

MACRO DATE MANIPULATION - please help!

All, I have seen and read many resources on date manipulation in SAS format specifically, but they have not helped me with this issue at work.  I have tried the Sysevalf and the Intnx feature and neither seem to give me what I am looking for.  If anyone can shed some light on this I would be very grateful. 

Simply put, I am trying to create a set of macros to use in an SQL step which all feed off of one date versus typing in each date separately.  Currently I have the below...

%let Related_Act_Date1 = 201212;

%let Related_Act_Date2 = 201301;

%let Related_Act_Date3 = 201302;

%let Related_Act_Date4 = 201303;

%let Related_Act_Date5 = 201304;

%let Related_Act_Date6 = 201305;

%let Related_Act_Date7 = 201306;

%let Related_Act_Date8 = 201307;

%let Related_Act_Date9 = 201308;

%let Related_Act_Date10 = 201309;

%let Related_Act_Date11 = 201310;

%let Related_Act_Date12 = 201311;

%let Related_Act_Date13 = 201312;  /*current month*/

Ideally I would like to just type in the current month and have all the others feed from that versus typing them in monthly.  This list is much longer, but I have only copied what is hopefully necessary to get my question across.  Hopefully you can see how big of a pain it is to change these every month.  I need to keep this format YYYYMM and day is not needed.  Can anyone shed some light on this by chance?

Thanks so much!


Accepted Solutions
Solution
‎02-17-2014 11:29 AM
Super User
Super User
Posts: 7,074

Re: MACRO DATE MANIPULATION - please help!

%SYFUNC() and INTNX() are what you want. Try this:

%let baseyymm = 201212 ;

%let basedate = "%sysfunc(inputn(&baseyymm.01,yymmdd8.),date9.)"d;

%put basedate=&basedate;

%let offset=1 ;

%let newyymm = %sysfunc(intnx(month,&basedate,&offset),yymmn6.);

%put newyymm=&newyymm;

%let offset=14 ;

%let newyymm = %sysfunc(intnx(month,&basedate,&offset),yymmn6.);

%put newyymm=&newyymm;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,931

Re: MACRO DATE MANIPULATION - please help!

So if I'm understanding properly ... you want to type in 201312 and get the 13 macro variables as shown? Or type in some other year/month and get the appropriate 13 macro variables for that other year/month?

UNTESTED CODE

%let currentmonth=201312;

data _null_;

    currmonth=mdy(substr("&currentmonth",5,2),1,substr("&currentmonth",1,4));

    do i=1 to 13;

          monthsago=13-i;

          datemonthsago=intnx('month',currmonth,-monthsago);

          call symputx('Related_Act_Date'||left(i),cats(year(datemonthsago),put(month(datemonthsago),z2.)));

     end;

run;

Solution
‎02-17-2014 11:29 AM
Super User
Super User
Posts: 7,074

Re: MACRO DATE MANIPULATION - please help!

%SYFUNC() and INTNX() are what you want. Try this:

%let baseyymm = 201212 ;

%let basedate = "%sysfunc(inputn(&baseyymm.01,yymmdd8.),date9.)"d;

%put basedate=&basedate;

%let offset=1 ;

%let newyymm = %sysfunc(intnx(month,&basedate,&offset),yymmn6.);

%put newyymm=&newyymm;

%let offset=14 ;

%let newyymm = %sysfunc(intnx(month,&basedate,&offset),yymmn6.);

%put newyymm=&newyymm;

New Contributor
Posts: 3

Re: MACRO DATE MANIPULATION - please help!

Tom -

Thank you so much for your reply, that is EXACTLY what I needed.  From reading many of your other responses, you are nothing short of genius with this stuff!  Thanks again so much!

Michael

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 472 views
  • 4 likes
  • 3 in conversation