1
votes

I want to create a SAS macro that creates symbols for this year and last year and last month's month number. So far I have:

%let LastMonth = %sysfunc(intnx(month,%sysfunc(date()),-1),DATE9.); 
%let ThisMonth = %sysfunc(intnx(month,%sysfunc(date()),0),DATE9.); 
%let ThisYear = %sysfunc(intnx(year,%sysfunc(date()),0),DATE9.); 
%let LastYear = %sysfunc(intnx(year,%sysfunc(date()),-1),DATE9.); 

These give: %put &LastYear; -> 01JAN2013 %put &ThisYear; -> 01JAN2014 %put &LastMonth; -> 01JAN2014 %put &ThisMonth; -> 01FEB2014

I am having trouble extracting the year and month numbers so that &LastMonth I'd like to get 1 (For January) and &Last year 2013 etc.

For the months I tried using the month() function:

%let LastMonth = %sysfunc(month(%sysfunc(intnx(month,%sysfunc(date()),-1),DATE9.))); 

but this gives an error and I don't understand why it occurs as :

ERROR: Argument 1 to function MONTH referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list.  Execution of %SYSCALL statement or %SYSFUNC or
   %QSYSFUNC function reference is terminated.

Any help appreciated!

2
Doubel check the date9. format you are applying during macro assignment for last year & month.user1509107

2 Answers

1
votes

user102890 is right - you were effectively trying to execute month(01JAN2014) instead of month("01JAN2014"d).

try removing the format altogether:

%put %sysfunc(month(%sysfunc(intnx(month,%sysfunc(date()),-1))));
1
votes

For the sake of code-readability and maintainence , I would recommend creating your dates in a seperte data step and then once you are happy with the dates you have generated then it is easy to create macro variables. For ex:In the following dataset we are setting up several date variables in refrence to referenceDate which is set to today();

/*set-up your dates*/
data dates;
referenceDate =today();
lastMonth = put(intnx('month', referenceDate, -1), date9.);
thisMonth = put(intnx('month', referenceDate, 0), date9.);
thisYear=put(intnx('year', referenceDate, 0), date9.);
lastYear=put(year(intnx('year', referenceDate, -1)),4.);
format referenceDate date9.;
run;

All your dates in the above dataset will be on a single line. Transposing the above dataset we will get a dataset which looks like the following:

/*transpose so that the dates you want to macrotise are on a seprate line*/
proc transpose data = dates out = date_t (rename = (_name_ =dateType col1=dateValue));
var _character_;
copy referenceDate;
run;

referenceDate   dateType    dateValue
17Feb2014      lastMonth    01JAN2014
               thisMonth    01FEB2014
               thisYear     01JAN2014
               lastYear     2013

Now, finally set up the macro's.

/*set-up macros*/
data _null_;
set date_t;
call symputx(dateType, dateValue);
run;

%put _user_;

In the log:

GLOBAL LASTYEAR 2013
GLOBAL THISYEAR 01JAN2014
GLOBAL LASTMONTH 01JAN2014
GLOBAL THISMONTH 01FEB2014