BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raivester
Quartz | Level 8

I am attempting to take the number of days between a start date (January 1, 2019) and an end date (January 1, 2019 + some length of time) and calculate and concatenate the two-digit number of years, months, and days. For example, a time period of 440 days is 1 year, 2 months and 15 days--the resultant number I would expect to get in the "duration" variable would be: 010215. I attempt this by running the following, but the month variable does not convert to character. Any ideas?

 

data out;
  set in;
  	length duration $6

	** create an end date variable to calculate years, days, and months between Jan 1 2019 and end date based on total days;
	date_end = "1JAN2019"d+total_days;

	yr    = put(intck('year',"1JAN2019"d,date_end,'c'), z2.);
	mntha = intck('month',intnx('year',"1JAN2019"d,yr,'s'),date_end,'c');
	daya  = date_end - intnx('month',"1JAN2019"d,mnth+yr*12,'s');

*Since I perform this operation below, I had to keep the temp month and day variables
numeric--I will change them to character with leading zeros after. Since I do not use
the year variable here, I converted this to char with leading zeros in the variable's
creation, above.; if daya=30 then do; mntha+1; daya=0; end; mnth = put(mntha, z2.); day = put(daya, z2.); duration=cats(yr,mnth,day); run;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
daya = date_end - intnx('month',"1JAN2019"d,mnth+yr*12,'s');

Should that mth be mntha?
Or does that mnth exist in data set already?

View solution in original post

2 REPLIES 2
Reeza
Super User
daya = date_end - intnx('month',"1JAN2019"d,mnth+yr*12,'s');

Should that mth be mntha?
Or does that mnth exist in data set already?
raivester
Quartz | Level 8

OH gosh! That's embarrassing! Thank you for catching that. I guess I have been staring too long--should have walked away and come back.