I have this code:
Don't work with dates as strings; work with dates as numbers and then format the number to appear the way you want.
We can't run your code, because we don't have the macro %run_setdate. I don't see anywhere that you are adding 30 days. And in your code you are doing things not described in your statement above.
Anyway, this works for me as a general method for adding 30 days to numeric dates.
data want; today = date(); add_30=today+30; run;
Create END_DT as a numeric date value, then adding 30 days is trivial; and then convert it to a macro variable value if needed. So all of your code shown below, I would draw a big red X through it, don't do it with text strings in macro variables.
%let year = %CMPRES(¤t_year.); %let begindt=%str(%')&year.0101%str(%'); %let enddt=%str(%')&year.0630%str(%'); %end; %else %if &sysparm = sixmonth and ¤t_month <7 %then %do; %let year = %CMPRES(&prior_year.); %let begindt=%str(%')&year.0701%str(%'); %let enddt=%str(%')&year.1231%str(%');
So if ENDDT is the last day of the year or last day of the half-year, and you want to add 30 days
data _null_; today=date(); enddt=intnx('semiyear',today,0,'e'); enddt_plus_30=enddt+30; call symputx('enddt_plus_30',enddt_plus_30); run;
As stated, you can format &ENDDT_PLUS_30 any way you want, but macro variables usually should not be formatted (unless they are meant to be used in titles, labels or other places where humans have to read the date) Please note that everything is done on numeric variables in a SAS date step; and not via pulling strings apart and combining them back together in macro variables.
If you start with a date value, such as your Today variable, the second worst thing to do with it prior to most manipulation is to separate out the parts of the date. The very worst is to separate the parts out as text values.
With a date value you increment dates by many standard intervals using the INTNX function.
A brief example:
data example; today=today(); startnextmonth = intnx('month',today,1,'b'); endnextmonth = intnx('month',today,1,'e'); startthisquarter = intnx('quarter',today,0,'b'); endthisquarter = intnx('quarter',today,0,'e'); startthishalfyear = intnx('semiyear',today,0,'b'); endthishalfyear = intnx('semiyear',today,0,'e'); format _numeric_ date9.; run;
The above uses month, quarter and semiyear (half of the year). The number after the date value is the number of intervals to increment, 0 is for the current, -1 is previous, 10 would 10 intervals later. The letter 'b' is beginning of interval, 'e' is end, and 's' is same which would advance a month to the same day of the month with all those caveats about leap days and variable month lengths. Other standard date intervals are semimonth, year and week (with some options as to how to treat week starts), weekday (increments to days Monday through Friday).
There are also shifts and offsets that can be used. So many common changes can be done with INTNX and the date value. IF you need text after that then apply a format to the value in creating the text.
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.