BookmarkSubscribeRSS Feed
ambadi007
Quartz | Level 8

Dear Experts,

 

I am working on a project newly and that needs to be impute a date asper the requirement 

 

Stop date imputation:- Impute stop date as latest possible date (last day of the particular month if day is unknown or 31December if the day and month are unknwon)

I am having doubt how to get the last day of the month since every month has different last day and we need to consider the leap year also 

 

start date :-mpute stop date as earliest possible date (first day of the particular month if day is unknown or 1st january if the day and month are unknwon)

 

 

dates will be like below 

Stop date             Start date
2019-03               2018-03
2019                    2017
2018-03-25          2017-02-22

 

2 REPLIES 2
Kurt_Bremser
Super User

Expand the strings as needed, and use the intnx() function to determine the last day of a given month:

data want;
input (stop_string start_string) (:$10.);
if length(start_string) = 4 then start_string = cats(start_string,'-01');
if length(start_string) = 7 then start_string = cats(start_string,'-01');
start_date = input(start_string,yymmdd10.);
if length(stop_string) = 4 then stop_string = cats(stop_string,'-12');
if length(stop_string) = 7
then do;
  stop_string = cats(stop_string,'-01');
  stop_date = intnx('month',input(stop_string,yymmdd10.),0,'e');
end;
else stop_date = input(stop_string,yymmdd10.);
format start_date stop_date yymmddd10.;
drop start_string stop_string;
datalines;
2019-03               2018-03
2019                    2017
2018-03-25          2017-02-22
;
Reeza
Super User
INTNX() will allow you to get the last day of the month using the alignment parameter. Use the first as your day and then use the 'e' as the last parameter to increment to the end of the month.

last_month = intnx('month', today(), 0, 'e');

This would return March 30, 2020.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 11050 views
  • 0 likes
  • 3 in conversation