How do I declare a specific date that I want to use throughout my code. I have three date fields and only want to type the date once. Here is wht I use in sql and want to translate it in sas.
Declare @Start_Date DAte
set @Start_Date = '2018-01-01'
I want to use throughout my code ...
It sounds like you want a macro variable that can be used in any data step or PROC or with other macro commands. Is that correct?
%let date = %sysfunc(mdy(1,1,2018));
Then any place you use &date, the proper date value will be inserted and used by your code. Example, assuming there is a variable named DATE in MYDB:
proc sql;
create table want as select * from mydb where date > &date;
quit;
I want to use throughout my code ...
It sounds like you want a macro variable that can be used in any data step or PROC or with other macro commands. Is that correct?
%let date = %sysfunc(mdy(1,1,2018));
Then any place you use &date, the proper date value will be inserted and used by your code. Example, assuming there is a variable named DATE in MYDB:
proc sql;
create table want as select * from mydb where date > &date;
quit;
do you mean you want to know the syntax of a sas date constant
data want;
date='01jan2018'd;
run;
At the top of the program, code:
%let start_date = %sysfunc (inputn (2018-01-01, yymmdd10)) ;
Then, anywhere in the program downstream, refer to it as &start_date. No quotes! For example:
data _null_ ;
new_date = &start_date + 10 ;
put new_date=yymmdd10. ;
run ;
You'll see in the SAS log:
new_date=2018-01-11
Remember, in SAS date is not a character string but an integer equal to the number of days since the beginning of 1960 (or before that point, in which case it's negative). The date of 1960-01-01 has the value of 0.
Paul D.
@hashman May i request your opinion on this style
%let start_date = 01jan2018 ;
data _null_ ;
new_date = "&start_date"d + 10 ;
put new_date=yymmdd10. ;
run ;
Coz it appears the macro data is anyway hardcoded?
@novinosrin wrote:
@hashman May i request your opinion on this style
%let start_date = 01jan2018 ;
data _null_ ;
new_date = "&start_date"d + 10 ;
put new_date=yymmdd10. ;
run ;
Coz it appears the macro data is anyway hardcoded?
It's more effort (and also more likelihood of mis-typing) if quotes are required. And probably just a hair slower as each time SAS has to convert 01jan2018 to an internal date value.
Thank you @PaigeMiller Point taken and learned something new. I didn't know the constant would under perform. Interesting insight!
Plus I was of wrong assumption, a macro processor is a touch slower than data step compiler especially when it came to interfaces i.e using conventional data step functions. The pause caused by the macro processor while tokenisation and then interface activity and then back to tokenisation before all processed tokens are moved to compiler for execution is what took me by surprise.
I have no problem with this style, but if I used it, I'd rather code:
%let start_date = '01jan2018'd ;
and used it in the subsequent code just as &start_date, so that I wouldn't have to quote and add the literal suffix D every time. It also leaves less interpretive work to the compiler. The reason I offered:
%let start_date = %sysfunc (inputn (2018-01-01, yymmdd10)) ;
is merely because the OP seems to prefer this input format since this is what was used in the original post.
Best
Paul D.
Thank you @hashman I know why you answered as pointed to the needs of OP. But you know me by now, I just jumped into learn something extra. I am cheeky. Can't help it
@hashman wrote:
I have no problem with this style, but if I used it, I'd rather code:
%let start_date = '01jan2018'd ;
and used it in the subsequent code just as &start_date, so that I wouldn't have to quote and add the literal suffix D every time. It also leaves less interpretive work to the compiler. The reason I offered:
%let start_date = %sysfunc (inputn (2018-01-01, yymmdd10)) ;
is merely because the OP seems to prefer this input format since this is what was used in the original post.
Best
Paul D.
And quite often I use the macro variable an just the date9 format 01JAN2018 because I then use the same macro variable in Title or Footnotes as well as the filter statements such as startdate ge "&date."d and/or document names. But what works for very simple code, many of my ad hoc requests are somewhat repetitive except for dates so include the specific requested dates in usually a title statement, may not be appropriate for much manipulation.
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 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.