I am writing a macro function that takes dates as input. Here is an example demonstrating what I would need:
/* In "General", this is how the macro should work. Example available below */
%macro MyDemonstrationMacro(Arg1, Arg2)
%let FirstDate = &Arg1. /* Same as first argument */
%let SecondDate = &Arg2. /* Same as second argument */
%let Third_Date = Function(Arg1) /* Follows from first argument BUT another format and first day in the month */
%let Fourth_Date = Function(Arg2) /* Follows from second argument BUT another format and first day in the month AFTER */
%mend MyDemonstrationMacro
/*Example: This is more concretly the rules and values that I would like to extract. */
%macro MyDemonstrationMacro(202106, 202110)
%let FirstDate = 202106 /* Same as first argument */
%let SecondDate = 202110 /* Same as second argument */
%let Third_Date = "01jun2021"d /* Follows from first argument BUT another format and first day in the month */
%let Fourth_Date = "01nov2021"d /* Follows from second argument BUT another format and first day in the month AFTER */
%mend MyDemonstrationMacro
Any advice on how I could achieve this functionality, i.e. getting the Third_Date and Fourth_Date with the input that I provide? This is obviosuly possible since it follows a logical rule.
@SasStatistics wrote:
I am writing a macro function that takes dates as input. Here is an example demonstrating what I would need:
/* In "General", this is how the macro should work. Example available below */
%macro MyDemonstrationMacro(Arg1, Arg2) %let FirstDate = &Arg1. /* Same as first argument */ %let SecondDate = &Arg2. /* Same as second argument */ %let Third_Date = Function(Arg1) /* Follows from first argument BUT another format and first day in the month */ %let Fourth_Date = Function(Arg2) /* Follows from second argument BUT another format and first day in the month AFTER */ %mend MyDemonstrationMacro
/*Example: This is more concretly the rules and values that I would like to extract. */
%macro MyDemonstrationMacro(202106, 202110) %let FirstDate = 202106 /* Same as first argument */ %let SecondDate = 202110 /* Same as second argument */ %let Third_Date = "01jun2021"d /* Follows from first argument BUT another format and first day in the month */ %let Fourth_Date = "01nov2021"d /* Follows from second argument BUT another format and first day in the month AFTER */ %mend MyDemonstrationMacroAny advice on how I could achieve this functionality, i.e. getting the Third_Date and Fourth_Date with the input that I provide? This is obviosuly possible since it follows a logical rule.
You can use %SYSFUNC(input(&arg1,yymmn6.)) to read in 202106 as an actual date. From there
Advice ... leave macro variables as unformatted unless you need them for titles, labels or file names. If you are performing arithmetic or logical operations with your macro variables, leave them unformatted!
So
%let Third_Date = %sysfunc(input(&arg1,yymmn6.));
is the unformatted date.
Adding: since you have explicitly requested advice above, here is my advice. In your thread from a few days ago, I gave you this advice:
Please remember, @SasStatistics , that if you format the macro variable value, and you want to use it for arithmetic or logical purposes, then you will have to un-format it. That's two operations (formatting followed by un-formatting, and don't forget to do both, and don't get one of the operations wrong) instead of zero operations. Either will work if done properly, it's your choice.
I see you didn't take that advice, and you are still struggling with the same issues. My advice remains the same, leave your macro variables unformatted.
Thanks, I am writing a special program that in the end will need the formatted dates.
Do you also know how I would shift the second argument 1 step, i.e.
How would I do the logical computation to move 202110 to "01nov2021" ?
@SasStatistics wrote:
Thanks, I am writing a special program that in the end will need the formatted dates.
Do you also know how I would shift the second argument 1 step, i.e.
How would I do the logical computation to move 202110 to "01nov2021" ?
Give an example of code (without macros and without macro variables) that needs formatted macro variables or text that has to be 01NOV2021 ... show us the whole code for the PROC or DATA step without macros and without macro variables.
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.