Hello,
I have some issues with the format of my macro variables.
I have a program where I have to determine around 15 macro variables by hand and I am trying to automate the process.
One of such variables would be:
%let ddebdet='01jan2016'd;
All of those 15 variables refer to different dates. I am trying to automate the process using the following code:
%let year=2018;
%let month=11;
data _null_;
call symputx('ddebdet', put(intnx('day',mdy(12,31,&year-3),0)+1,date9.));
run;
The idea is to adapt only two variables. Year and month, in this case and to generate 01jan2016 automatically.
This is the output that I'm getting:
2275 %put &ddebdet;
01JAN2016
2276
2277 %let ddebdet='01jan2016'd;
2278 %put &ddebdet;
'01jan2016'd
2279
2280
2281 %let year=2018;
2282 %let month=11;
2283
2284 data _null_;
2285 call symputx('ddebdet', put(intnx('day',mdy(12,31,&year-3),0)+1,date9.));
2286 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
2287
2288
2289 %put &ddebdet;
01JAN2016
As you can see there is a difference between
2278 %put &ddebdet;
'01jan2016'd
and
2289 %put &ddebdet;
01JAN2016
It is quite problematic because the rest of my code doesn't work with the auto generated solution. For example this
data det;
set data
(where=(&ddebdet<=datepart(dtismaj)<=&dfindet and ctyp='350'));
run;
Returns the following error:
NOTE: Line generated by the macro variable "DDEBDET".
1230 01JAN2016
-------
22
76
ERROR: Syntax error while parsing WHERE clause.
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>,
=, >, >=, AND, EQ, GE, GT, LE, LT, NE, OR, ^=, |, ||, ~=.
ERROR 76-322: Syntax error, statement will be ignored.
I don't really understand why I am having this issue.
Thanks in advance.
The "why" you are getting the results is easy. Within a SAS program, this is the right way to refer to a specific date:
'01jan2016'd
This is not a date, it is just a text string:
01jan2016
The question becomes what to do about it. While there are a number of ways to correct the problem, one way would amount to a small change. At the point where you have generated this as the value of your macro variable:
01jan2016
Add a statement to modify the value:
%let ddebdet = "&ddebdet"d;
Use doublequotes, not single quotes. That changes your macro variable into an acceptable form to represent a date.
There are other ways to simplify the program, but this would be the easiest fix.
The "why" you are getting the results is easy. Within a SAS program, this is the right way to refer to a specific date:
'01jan2016'd
This is not a date, it is just a text string:
01jan2016
The question becomes what to do about it. While there are a number of ways to correct the problem, one way would amount to a small change. At the point where you have generated this as the value of your macro variable:
01jan2016
Add a statement to modify the value:
%let ddebdet = "&ddebdet"d;
Use doublequotes, not single quotes. That changes your macro variable into an acceptable form to represent a date.
There are other ways to simplify the program, but this would be the easiest fix.
Adding to what @Astounding said, generally you would not want to format the macro variables at all. Just use straight unformatted SAS date values or unformatted SAS date/time values.
The only times you would want formatting is for human readability when outputting results, or human readability when inputting results. But its less work to code these things without the formatting for use in the macro processor, where human readability is not required.
Hello,
When you have to filter dates in SAS then you need to pass a date value, i.e. the value you have passed need to be understood by SAS. Leave the date value as numeric without formatting it, if you want to use a formatted value then convert that numeric value into your formatted vaue.
%let year=2018;
%let month=11;
%let date=%sysfunc(mdy(&month,01,&year));
%put &=date;
%let date_formated=%sysfunc(mdy(&month,01,&year),mmddyy10.);
%put &=date_formated;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.