BookmarkSubscribeRSS Feed
Sheeba
Lapis Lazuli | Level 10

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

6 REPLIES 6
Sheeba
Lapis Lazuli | Level 10

Hi  ,

 

Thanks a lot for the reply.

 

I used the INTNX  function and got the required results

 

Regards,

Sheeba

ballardw
Super User

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.

Sheeba
Lapis Lazuli | Level 10

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

Tom
Super User Tom
Super User

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;
Sheeba
Lapis Lazuli | Level 10

Hi Tom,

 

Thanks a lot for the details and code.

 

I used this and is working successfully.

 

Thanks,

 

Regards,

Sheeba

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 5470 views
  • 3 likes
  • 4 in conversation