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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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.;

View solution in original post

8 REPLIES 8
Reeza
Super User
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.;
Bluebonnet16
Fluorite | Level 6

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!!!

PaigeMiller
Diamond | Level 26

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

 

 

--
Paige Miller
Bluebonnet16
Fluorite | Level 6

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.

Reeza
Super User
You can replace it with the unformatted macro variable, it'll still work fine. Try it if you don't believe us.

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
Bluebonnet16
Fluorite | Level 6
And you're right! I'll work on simplifying my code. Thank you for the feedback!!
Reeza
Super User
That is what gets created. Check the log.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1548 views
  • 7 likes
  • 3 in conversation