BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
asherer
Obsidian | Level 7

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'

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
novinosrin
Tourmaline | Level 20

do you mean you want to know the syntax of a sas date constant

 

data want;

date='01jan2018'd;

run;

hashman
Ammonite | Level 13

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.

novinosrin
Tourmaline | Level 20

@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?

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
novinosrin
Tourmaline | Level 20

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. 

hashman
Ammonite | Level 13

@novinosrin:

 

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.

novinosrin
Tourmaline | Level 20

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 

ballardw
Super User

@hashman wrote:

@novinosrin:

 

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.

hashman
Ammonite | Level 13

@ballardw:

 

That which fits one's purpose best is what needs to be done. 

 

Best

Paul D.

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