BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mona4u
Lapis Lazuli | Level 10

 

Hi All, 

I'm trying to create macros that create month names

l need the macro to be able to create the exact last month even though if it last year.

last time I created this macro to create last month # by the macro below  but I also need the month name too 

%let lastmonth1=%sysfunc(month(%sysfunc(intnx(month,%sysfunc(today()),-1,s))));

 

 

 %put &lastmonth1;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use the MONNAME. format in the SYSFUNC() as the second parameter. See the doucmentstion for INTNX and SYSFUNC so you can see how to use it. 

 


@mona4u wrote:

 

Hi All, 

I'm trying to create macros that create month names

l need the macro to be able to create the exact last month even though if it last year.

last time I created this macro to create last month # by the macro below  but I also need the month name too 

%let lastmonth1=%sysfunc(month(%sysfunc(intnx(month,%sysfunc(today()),-1,s))));

 

 

 %put &lastmonth1;


 

View solution in original post

5 REPLIES 5
Reeza
Super User

Use the MONNAME. format in the SYSFUNC() as the second parameter. See the doucmentstion for INTNX and SYSFUNC so you can see how to use it. 

 


@mona4u wrote:

 

Hi All, 

I'm trying to create macros that create month names

l need the macro to be able to create the exact last month even though if it last year.

last time I created this macro to create last month # by the macro below  but I also need the month name too 

%let lastmonth1=%sysfunc(month(%sysfunc(intnx(month,%sysfunc(today()),-1,s))));

 

 

 %put &lastmonth1;


 

Tom
Super User Tom
Super User

Use the WORDDAT format and parse out the name of the month.

%let lastmonth1=%scan(%sysfunc(intnx(month,%sysfunc(today()),-1),worddat),1) ;

 

mona4u
Lapis Lazuli | Level 10

didn't work

Tom
Super User Tom
Super User

@mona4u wrote:

didn't work


Two problems. (1) the format name is really WORDDATE (not sure when SAS started creating format names with more than 7 characters?). (2) The WORDDATE (and also the MONNAME) format add leading spaces which seem to really confuse the %SCAN() function when trying to process in single nested call. 

 

So use %Qsysfunc() instead to quote the comma and leading spaces.  Then the %SCAN() function works.

 

%let lastmonth=%scan(%qsysfunc(intnx(month,&today,-1),worddate),1);

 

 

mona4u
Lapis Lazuli | Level 10

@Tom wrote:

@mona4u wrote:

didn't work


Two problems. (1) the format name is really WORDDATE (not sure when SAS started creating format names with more than 7 characters?). (2) The WORDDATE (and also the MONNAME) format add leading spaces which seem to really confuse the %SCAN() function when trying to process in single nested call. 

 

So use %Qsysfunc() instead to quote the comma and leading spaces.  Then the %SCAN() function works.

 

%let lastmonth=%scan(%qsysfunc(intnx(month,&today,-1),worddate),1);

 this is what I tried and it works 

%let lastmonthn1=%sysfunc(intnx(month,%sysfunc(today()),-1),MONNAME.);

 


 

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