Hi Guys,
How to create date macro for below date
date='12-01-2023' (dd-mm-yyyy) format
/*i tried this way is it correct ? or any other method is there like using %sysfunc and putN and inputN functions please suggest */
%let input_date = 12-01-2023;
%put &input_date;
it is interview question
how to create a macro variable for date input -'12-01-2023' ?
If that is the whole wording of the question, you have already answered it, because all you need is the simple %LET.
Please post the whole, exact wording of the question, if there's more to it.
@BrahmanandaRao wrote:
it is interview question
how to create a macro variable for date input -'12-01-2023' ?
So first tell the interviewer that macro variables just contain text strings and the meaning of those strings is determined by how you use them. Also ask what is the source of value to place in the macro variable.
If the goal is to use that value to generate code in pass thru SQL then perhaps it might make some since to store a quoted string in MM-DD-YYYY (or is that DD-MM-YYYY) style, if that is the syntax that the remoted database being queried uses.
But if the goal is to use the value in SAS code to represent a date then you should either set the macro variable to a date literal such as
%let date='01DEC2023'd;
%let date="01DEC2023"d;
%let date='01-DEC-2023'd;
%let date="1-dec-2023"d;
etc.
Or just to the actual number of days since 1960 that SAS uses to store dates.
The other question is what is the source of the date? Why 12-01-2023 and not some other date? If it is user input then just tell the user to type in the value in the agreed style. If the goal is to generate the macro variable from date calculations then the only tricky thing about generating that particular style of quoted string would be if you were required to use the single quotes instead of double quotes (as many database systems syntax requires because they would treat double quotes as indicating an object NAME instead of date or string constant). That can be tricky if you want to generate the string in pure macro code as the macro processor ignores macro code that is enclosed in single quotes instead of double quotes. In that case %BQUOTE() is your friend.
For example if you wanted to set the macro variable DATE to first day of the last month of the current year and store the value in that MDY order with single quotes you might use this type of code in SAS macro code.
%let date=%sysfunc(today());
%let date=%sysfunc(intnx(year,&date,0,e));
%let date=%sysfunc(intnx(month,&date,0,e),mmddyyd10.);
%let date=%bquote('&date');
Get today's date (leave it a simple digit string representing the number of days since 1960).
Find the last day of the year.
Find the first day of the month, and store it in the style generated by the MMDDYYD10 format.
Add quotes around the value.
You might want to also add a call to %UNQUOTE() to remove the macro quoting added by the %BQUOTE() function.
Note if you know you always want 12-01 then just hard code it and skip the more complex date manipulation.
%let date=%bquote('12-01-%sysfunc(date(),year4.)');
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.