This code represents a call symput macro that formats the last day of the previous month. I include the output symbolgen
data _null_;
call symput('m',put(&pbd_date,yymmdd10.));
run;
%put &m;
SYMBOLGEN: Macro variable M resolves to 2014-09-30
10648 %put &m;
2014-09-30
Now I want to use &m to define the previous month and the last 2 months consecutively. The data _null_ accomplished that with no issue
However if you look at the information in red, when I use the put statements I get March and April 1965 not 2014. I am scratching my head here. For those who have worked with date variables any suggestions
data _NULL_;
call symput ( 'curr_month1', cat("'"||put(intnx('month',&m.,-1,'e'),yymmdd10.)||"'"));
call symput ( 'prev_month1', cat("'"||put(intnx('month',&m.,-2,'e'),yymmdd10.)||"'"));
run;
%put &curr_month1 &prev_month1;
10611 call symput ( 'curr_month1', cat("'"||put(intnx('month',&m.,-1,'e'),yymmdd10.)||"'"));
SYMBOLGEN: Macro variable M resolves to 2014-09-30
10612 call symput ( 'prev_month1', cat("'"||put(intnx('month',&m.,-2,'e'),yymmdd10.)||"'"));
SYMBOLGEN: Macro variable M resolves to 2014-09-30
10613 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
SYMBOLGEN: Macro variable CURR_MONTH1 resolves to '1965-04-30'
SYMBOLGEN: Macro variable PREV_MONTH1 resolves to '1965-03-31'
10614 %put &curr_month1 &prev_month1;
'1965-04-30' '1965-03-31'
If the only thing you are going to use &m for is to create those values then don't bother. You have a string which the date functions won't recognize correctly. Hint: do the arithmetic 2014 minus 09 minus 30 and display the result using a date format..
Use the &pbd_date instead in your current and previous month calculations.
I tried the code below, but the macro put statement doesn't produce the desired result 2014/10/08 in the macro variable m. Do I need to change any formats?
15 options symbolgen;
16
17 %let pbd_date= 2014/10/08;
18 data _null_;
19
20 call symput('m',put(&pbd_date,yymmdd10.));
SYMBOLGEN: Macro variable PBD_DATE resolves to 2014/10/08
21
22 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
23
24 %put &m;
SYMBOLGEN: Macro variable M resolves to 1960-01-26
1960-01-26
I believe that you haven't fully understood yet how SAS dates work and that this is the reason for your struggle. The following link should help you to overcome this hurdle SAS(R) 9.4 Language Reference: Concepts, Third Edition
And here some code which should do what you're after. I personally wouldn't add the quotes to the date string. You can do this later when using the macro variable.
%let pbd_date=%sysfunc(inputn(2014/10/08,yymmdd10.));
data _null_;
call symput('m',put(&pbd_date,yymmdd10.));
call symput('curr_month1', cats("'",put(intnx('month',&pbd_date.,-1,'e'),yymmdd10.),"'"));
call symput('prev_month1', cats("'",put(intnx('month',&pbd_date.,-2,'e'),yymmdd10.),"'"));
call symputx('curr_month2', put(intnx('month',&pbd_date.,-1,'e'),yymmdd10.));
call symputx('prev_month2', put(intnx('month',&pbd_date.,-2,'e'),yymmdd10.));
run;
%put &pbd_date &m;
%put &curr_month1 &prev_month1;
%put &curr_month2 &prev_month2;
Use the value of &pgd_date In this code you posted earlier:
data _null_;
call symput('m',put(&pbd_date,yymmdd10.));
run;
since M resolved to 2014-09-30 then &pdd_date was the proper date value.
But when you do:
%let pbd_date= 2014/10/08;
You are assigning it a completely different value. Why? Where did the original &pbd_date come from? USE IT.
Hello,
The reason for getting the incorrect values is the using of text value provided by the macro variable &m as the second argument of the function intnx. As per SAS documentation of intnx function:
specifies a SAS expression that represents a SAS date, time, or datetime value that identifies a starting point
One solution:
data _NULL_;
call symput ( 'curr_month1',
cat("'"||put(intnx('month',input("&m",yymmdd10.),-1,'e'),yymmdd10.)||"'"));
call symput ( 'prev_month1',
cat("'"||put(intnx('month',input("&m",yymmdd10.),-2,'e'),yymmdd10.)||"'"));
run;
In your current approach, you are converting a date(&pbd_date) to a string (&M). You are then trying to convert this date back to a string before performing calculations. You can greatly simplify the process if you use the original date .
data _null_;
call symput('m',put(&pbd_date,yymmdd10.));
call symput('curr_month1', put(intnx('month',(&pbd_date,- 1,'e'),yymmdd10.);
call symput('prev_month1', put(intnx('month',(&pbd_date,-2,'e'),yymmdd10.);
run;
The reason this works is because &m is not an actual date, but rather a string. If you want to stay with your previous approach, you can do that using an informat: input(&m,yymmdd10.).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.