BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mc14
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

%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

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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;

--
Paige Miller
Tom
Super User Tom
Super User

%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;

Mc14
Calcite | Level 5

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1661 views
  • 5 likes
  • 3 in conversation