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

 

I have the following dataset

 

data have;
input date_of_birth;
datalines;
19600101
190002..

200002..

199812..


;
run;

 

I want to write a program that will create a new dataset, that replaces ‘..’ by the last day of the respective month.

Note: I also want to account for leap years. Therefore, for the 2nd value (190002..), I would want this to read 19000229 because this is a Leap Year

Appreciate all help!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Read First 6 chars only as a char, convert it to a date using input and correct informat and then use INTNX to set it to the end of the month with the alignment option. 

 

 

Data want;

input date $6.;

date_end = intnx('month', input(date, yymm6.), 0, 'e');

format date_end yymmdd8.;

cards;

.....

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Read First 6 chars only as a char, convert it to a date using input and correct informat and then use INTNX to set it to the end of the month with the alignment option. 

 

 

Data want;

input date $6.;

date_end = intnx('month', input(date, yymm6.), 0, 'e');

format date_end yymmdd8.;

cards;

.....

 

Jagadishkatam
Amethyst | Level 16
@Reeza solution works fine, the only change is we need to use the informat yymmn6.
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16
data have;
infile datalines missover;
input date_of_birth $10.;
datalines;
19600101
190002
200002
199812
;
run;

data want;
set have;
month=substr(date_of_birth,5,2);
year=substr(date_of_birth,1,4);
day=substr(date_of_birth,7,2);


length date date_imp $50.; 
 
    if day='' then do;  
date=cats(strip(year),strip(put(input(month, best.),z2.)));  
if month="12" then date_imp=compress(put((mdy(1,1,input(year,best.)+1)-1), is8601da.),'-'); 
else date_imp=compress(put((mdy(input(month,best.)+1,1,input(year,best.))-1),is8601da.),'-');    
end;    
else do;       
date=compress(put(mdy(input(month,best.),input(day,best.),input(year,best.)),is8601da.),'-');  
date_imp=compress(date,'-');    
end;
run;
Thanks,
Jag
art297
Opal | Level 21
data have;
  input cdate_of_birth $;
  format date_of_birth date9.;
  cdate_of_birth=compress(cdate_of_birth,,'kd');
  if length(cdate_of_birth) lt 8 then date_of_birth=
   intnx('month',input(catt(cdate_of_birth,'01'),yymmdd8.),0,'e');
  else date_of_birth=input(cdate_of_birth,yymmdd8.);
  datalines;
19600101
190002..
200002..
199812..
;

Art, CEO, AnalystFinder.com

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 2624 views
  • 2 likes
  • 4 in conversation