DATA Step, Macro, Functions and more

how to extract first and last day from a month?

Reply
Regular Contributor
Posts: 162

how to extract first and last day from a month?

Hi,

 

I am creating a macro variable

%let month_to_process = july;

 

How do I extract the first day and last day of the month to be used in a series of if conditions below?

 

data elig_subset_08092017;

set elig_subset;

 

/*for july*/

if datepart(EFFECTIVE_DT)>'31jul2017'd then

do;

effective_date_july =.;

term_date_july =.;

end;

else if datepart(EFFECTIVE_DT) le '31jul2017'd and datepart(EFFECTIVE_DT) ge '01jul2017'd then

do;

if datepart(term_date)>'31jul2017'd then

do;

effective_date_july =datepart(EFFECTIVE_DT);

term_date_july ='31jul2017'd;

end;

else

do;

effective_date_july =datepart(EFFECTIVE_DT);

term_date_july =datepart(term_date);

end;

end;

else if datepart(EFFECTIVE_DT) < '01jul2017'd then

do;

if datepart(term_date)<'01jul2017'd then

do;

effective_date_july =.;

term_date_july =.;

end;

else if (datepart(term_date) ge '01jul2017'd) and (datepart(term_date) le '31jul2017'd) then

do;

effective_date_july = '01jul2017'd;

term_date_july =datepart(term_date);

end;

else if datepart(term_date)>'31jul2017'd then

do;

effective_date_july ='01jul2017'd;

term_date_july ='31jul2017'd;

end;

end;

run;

 

Appreciate any help.

 

Thanks ,

 

Regards,

Sheeba

Super User
Posts: 6,938

Re: how to extract first and last day from a month?

Use the intnx() function with 'month' and 'begin' and 'end'.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 162

Re: how to extract first and last day from a month?

Hi  ,

 

Thanks a lot for the reply.

 

I used the INTNX  function and got the required results

 

Regards,

Sheeba

Super User
Posts: 10,500

Re: how to extract first and last day from a month?

SAS supports comparisons like  5 < variable < 25

so you can make this code

else if datepart(EFFECTIVE_DT) le '31jul2017'd and datepart(EFFECTIVE_DT) ge '01jul2017'd then

 

into this:

else if '01jul2017'd le datepart(EFFECTIVE_DT) le '31jul2017'd  then

But even easier unless your data is multiyear:

 

  else if month(datepart(effective_dt))=7;
If you are going to use datepart(effictive_dt) so many places you might as well make a temporary variable to make code easier to read.

 

You might discuss the purpose of those If conditions.

Regular Contributor
Posts: 162

Re: how to extract first and last day from a month?

Hi ballardw,

 

Thanks a lot for the detailed reply and tips .

 

I added a variable to hold the date part and used the le operator as suggested.

 

I used the if conditions to check if the person is active for the month. so in case the person is active for a month, i just want the effective date and term date to be set as first and last of the month. so if a person is active starting from previous month to next month , i just want start date july and end date july as first and last of the month.

 

Thanks again

Regards,

Sheeba

Super User
Super User
Posts: 6,500

Re: how to extract first and last day from a month?

I am not sure you are starting with enough information.

%let month_to_process = july;

For what year do you want JULY dates for? 2017?  2016? 2018?

If you start with a date value.

%let month_to_process = '01JUL2017'd;

Then you could use INTNX() function to find the first and last day of that month.

%let firstday=%sysfunc(intnx(month,&month_to_process,0,b));
%let lastday=%sysfunc(intnx(month,&month_to_process,0,e));

Of if you would prefer human readable values then perhaps you could generate them as date literals.

%let firstday="%sysfunc(intnx(month,&month_to_process,0,b),date9)"d;
%let lastday="%sysfunc(intnx(month,&month_to_process,0,e),date9)"d;
Regular Contributor
Posts: 162

Re: how to extract first and last day from a month?

Hi Tom,

 

Thanks a lot for the details and code.

 

I used this and is working successfully.

 

Thanks,

 

Regards,

Sheeba

Ask a Question
Discussion stats
  • 6 replies
  • 207 views
  • 3 likes
  • 4 in conversation