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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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

4 REPLIES 4
Shmuel
Garnet | Level 18

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Reeza
Super User

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;
ballardw
Super User

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 5283 views
  • 0 likes
  • 5 in conversation