BookmarkSubscribeRSS Feed
ssitharath0420
Quartz | Level 8

Hello,

 

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);
run;
 
%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(%');
%end;
%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(%');
%end;
%else %do;
%run_setdate(freq=quarter);
%end;
%mend;
 
%datetest;
 
%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.
2 REPLIES 2
PaigeMiller
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();
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(&current_year.);
%let begindt=%str(%')&year.0101%str(%');
%let enddt=%str(%')&year.0630%str(%');
%end;
%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_;
     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.

--
Paige Miller
ballardw
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;
   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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 426 views
  • 2 likes
  • 3 in conversation