SAS changing today() to previous month and subsequent

Accepted Solution Solved
Reply
Contributor
Posts: 56
Accepted Solution

SAS changing today() to previous month and subsequent

Hello!

 

Can someone please help me

 

I have created a program where I have automated dates till reporting month. Reporting month is one month prior to the current month. Suppose current month is July

 

I have used

%do i=1 %to &month ;

%let date2=%sysfunc(intnx(month, %sysfunc(today()),-&i.,e), date9.);"&date2"n %end;

 

month macro variable i have declared till previous month which is coming 6 and used the do loop which is fetching me right results from jan to june this year.  The issue is in certain case if the user wants to have reporting month as May or april, how do I do that? this macro variable and macro is used at more than 100 places i can't go and change this everytime...I need some function or macro variable where it should take today() as previous month date or 2 months date prior to current month ... is it possible to define today() to previous month in macro variable.. That will solve my concern.


Accepted Solutions
Solution
‎07-26-2017 12:57 PM
Super User
Posts: 11,343

Re: SAS changing today() to previous month and subsequent

Posted in reply to sameer112217

sameer112217 wrote:

Hello!

 

Can someone please help me

 

I have created a program where I have automated dates till reporting month. Reporting month is one month prior to the current month. Suppose current month is July

 

I have used

%do i=1 %to &month ;

%let date2=%sysfunc(intnx(month, %sysfunc(today()),-&i.,e), date9.);"&date2"n %end;

 

month macro variable i have declared till previous month which is coming 6 and used the do loop which is fetching me right results from jan to june this year.  The issue is in certain case if the user wants to have reporting month as May or april, how do I do that? this macro variable and macro is used at more than 100 places i can't go and change this everytime...I need some function or macro variable where it should take today() as previous month date or 2 months date prior to current month ... is it possible to define today() to previous month in macro variable.. That will solve my concern.


Which macro variable? Month? Date2? Something else?

Are the "100 places" in one macro? many macros?

And I can't say that I have ever been happy with variables with names like "31JAN2017"n at that is usually and indication of poor data structure to begin with.

 

You might consider adding a keyword parameter to your macro calls that would conditionally override which ever bit of code you are concerned with and set the value of the date(?) variable.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,566

Re: SAS changing today() to previous month and subsequent

Posted in reply to sameer112217

The whole situation is not very clear to me.

 

I hope one of the two codes can help you:

data _null_;
    td = today();
/* -1- */ end_last_manth = td - day(td); /* -2- */ end_last_month = intbx('month',td,-1,'e'); call symput('date2', put(end_last_month, ...desired format ...); run;
Super User
Super User
Posts: 7,955

Re: SAS changing today() to previous month and subsequent

Posted in reply to sameer112217

Today() cannot be changed - otherwise it would become useless.  If you need a bit of text which is the month before today(), then set a macro varible with the information and use that throughout your code - this is the purpose of macro variables.

Super User
Posts: 19,805

Re: SAS changing today() to previous month and subsequent

Posted in reply to sameer112217

It looks like you count backwards from month to the beginning of the year. 

 

Try reversing that loop count so that you can start at the beginning of the period and count forward, it shouldn't require any other changes in your code. 

 

%do i=&month %to 1 %by -1;
Solution
‎07-26-2017 12:57 PM
Super User
Posts: 11,343

Re: SAS changing today() to previous month and subsequent

Posted in reply to sameer112217

sameer112217 wrote:

Hello!

 

Can someone please help me

 

I have created a program where I have automated dates till reporting month. Reporting month is one month prior to the current month. Suppose current month is July

 

I have used

%do i=1 %to &month ;

%let date2=%sysfunc(intnx(month, %sysfunc(today()),-&i.,e), date9.);"&date2"n %end;

 

month macro variable i have declared till previous month which is coming 6 and used the do loop which is fetching me right results from jan to june this year.  The issue is in certain case if the user wants to have reporting month as May or april, how do I do that? this macro variable and macro is used at more than 100 places i can't go and change this everytime...I need some function or macro variable where it should take today() as previous month date or 2 months date prior to current month ... is it possible to define today() to previous month in macro variable.. That will solve my concern.


Which macro variable? Month? Date2? Something else?

Are the "100 places" in one macro? many macros?

And I can't say that I have ever been happy with variables with names like "31JAN2017"n at that is usually and indication of poor data structure to begin with.

 

You might consider adding a keyword parameter to your macro calls that would conditionally override which ever bit of code you are concerned with and set the value of the date(?) variable.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 90 views
  • 0 likes
  • 5 in conversation