BookmarkSubscribeRSS Feed
Quartz | Level 8



I have this code:


data _null_;
today = date();
current_month = month(today);
current_year = year(today);
prior_year = current_year-1;
call symput('current_month',current_month);
call symput('current_year',current_year);
call symput('prior_year',prior_year);
%macro datetest;
%global begindt enddt;
%if &sysparm = sixmonth and &current_month >=7 %then %do;
%let year = %CMPRES(&current_year.);
%let begindt=%str(%')&year.0101%str(%');
%let enddt=%str(%')&year.0630%str(%');
%else %if &sysparm = sixmonth and &current_month <7 %then %do;
%let year = %CMPRES(&prior_year.);
%let begindt=%str(%')&year.0701%str(%');
%let enddt=%str(%')&year.1231%str(%');
%else %do;
%put &begindt;
%put &enddt;
I am trying to create a new variable Paid_Enddate by adding 30 days to the enddt.  The enddt is a string and I can't seem to convert the enddt to date and adding 30 days to it.  Any help will be appreciated.
Diamond | Level 26

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();


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(&current_year.);
%let begindt=%str(%')&year.0101%str(%');
%let enddt=%str(%')&year.0630%str(%');
%else %if &sysparm = sixmonth and &current_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_;
     call symputx('enddt_plus_30',enddt_plus_30);   


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.

Paige Miller
Super User

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

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.



Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.


Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 


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
  • 2 replies
  • 3 in conversation