BookmarkSubscribeRSS Feed
SasStatistics
Pyrite | Level 9

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. 


4 REPLIES 4
PaigeMiller
Diamond | Level 26

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



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.

--
Paige Miller
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
SasStatistics
Pyrite | Level 9

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

PaigeMiller
Diamond | Level 26

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

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 313 views
  • 4 likes
  • 2 in conversation