Hi ,
I have the following code below that has a hard-coded BeginDt and macro to create EndDate3, both of which are later used in my program and need to be in 'ddmmmyyyy'd format:
%let BeginDt = '01SEP2015'd;
%let Enddt = %sysfunc(today(),date9.);
%let Enddt2 ="&Enddt"d;
%let Enddt3=%sysfunc(tranwrd(&Enddt2,%str(%"),%str(%')));
%put &Enddt3;
I'm trying to modify the code, so the BeginDate is also dynamic based on a certain condition. The condition is that if EndDt3 is less that 01Sep(current year) then Startdate should be 01Sep(current year minus 5). Otherwise, if EndDt3 is greater or equal to 01Sep(current year) then StartDate should be 01Sep(current year minus 4). Ultimately, I need StartDate macro variable to look like '01sep2015'd (depending on a condition).
I have this code below and it's not working as it's not converting macro characters into date values:
data _null_;
%let Enddt = %sysfunc(today(),date9.);
%let Enddt2 ="&Enddt"d;
%let Enddt3=%sysfunc(tranwrd(&Enddt2,%str(%"),%str(%')));
%let curr_year=%sysfunc(year(%sysfunc(date())));
if &Enddt3 < '01sep&curr_year'd then start='01sep(&curr_year-5)'d;
else if &Enddt3 >= '01sep&curr_year'd then start='01sep(&curr_year-4)'d;
%let StartDt=start;
%put &StartDt;
run;
Any suggestions of how to solve this would be greatly appreciated.
Thank you!
data demo;
if month(today()) >= 9 then
year_start = year(Today()) - 5;
else year_Start = year(Today()) - 4;
date_start = mdy(9, 1, year_start);
date_end = today();
call symputx('date_end', catt(quote(put(date_end, date9.)), 'd'));
call symputx('date_start', catt(quote(put(date_start, date9.)), 'd'));
run;
%put &date_end.;
%put &date_start.;
data demo;
if month(today()) >= 9 then
year_start = year(Today()) - 5;
else year_Start = year(Today()) - 4;
date_start = mdy(9, 1, year_start);
date_end = today();
call symputx('date_end', catt(quote(put(date_end, date9.)), 'd'));
call symputx('date_start', catt(quote(put(date_start, date9.)), 'd'));
run;
%put &date_end.;
%put &date_start.;
Thank you! The code is easy to understand and works. But the final %put macros are in "01SEP2015"d, but that I can fix! Thank you SO much!!!
Your coding would be so much easier if you just didn't format the macro variables.
%let Enddt = %sysfunc(today());
and then you wouldn't have to go through the trouble to surround it in quotes, and later change the quotes to double-quotes, and you wouldn't have to put the letter D after the quotes, and this simplicity carries through the entire code. Maxim 28
yes, it would have been easier, but my program is using these 2 date macros to pull the data from other datasets and just works in 'ddmmmyyyy'd format.
@Bluebonnet16 wrote:
yes, it would have been easier, but my program is using these 2 date macros to pull the data from other datasets and just works in 'ddmmmyyyy'd format.
Just because you receive the data in a certain form doesn't mean you have to choose the most difficult way to continue the programming.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.