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

Hi Team,

 

I need help in programming part for date imputation. Here is the raw data below:

Raw data:

subj       date

101        2020-01-28      

102        2020-01

103        2020-02

104        2020-04

105        2020

 

I need to do date imputation based on two points as below:

    1.  If both month and day are missing, then set to December 31.
     2. If only day is missing, then set to last day of the month.

Output should be as below:

OUTPUT data:

subj       date

101        2020-01-28      

102        2020-01-31

103        2020-02-28

104        2020-04-30

105        2020-12-31

 

Could you please provide help in programming part. TIA.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Try this:

 

data have;
input subj d :$10.;
datalines;
101        2020-01-28      
102        2020-01
103        2020-02
104        2020-04
105        2020
;

data want;
set have;
year = input(scan(d, 1, "-"), ?? best.);
month = input(scan(d, 2, "-"), ?? best.);
day = input(scan(d, 3, "-"), ?? best.);
date = mdy(coalesce(month, 12), coalesce(day, 1), year);
if missing(day) then date = intnx("month", date, 0, "END");
format date yymmdd10.;
run;

proc print; run;

PGStats_0-1617243042924.png

 

PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

Try this:

 

data have;
input subj d :$10.;
datalines;
101        2020-01-28      
102        2020-01
103        2020-02
104        2020-04
105        2020
;

data want;
set have;
year = input(scan(d, 1, "-"), ?? best.);
month = input(scan(d, 2, "-"), ?? best.);
day = input(scan(d, 3, "-"), ?? best.);
date = mdy(coalesce(month, 12), coalesce(day, 1), year);
if missing(day) then date = intnx("month", date, 0, "END");
format date yymmdd10.;
run;

proc print; run;

PGStats_0-1617243042924.png

 

PG
mounikag
Obsidian | Level 7

Thank you so much. solution worked

qoit
Pyrite | Level 9

See below:

 

data want (drop=date new_var1);
	input subj$ date $10.;
	format date_var new_var1 yymmdd10.;

	if length(compress(date,"-")) = 4 then
		date_var = input(cats(date,"-12","-31"),yymmdd10.);
	else if length(compress(date,"-")) = 6 then
		do;
			new_var1 = input(cats(date,"-01"),yymmdd10.);
			date_var = intnx('month',new_var1,0,'e');
		end;
	else
		do new_var1 = .;
			date_var = input(date,yymmdd10.);
		end;

	datalines;
101 2020-01-28
102 2020-01
103 2020-02
104 2020-04
105 2020
;
run;
mounikag
Obsidian | Level 7

Thank you so much. solution worked

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
  • 4757 views
  • 4 likes
  • 3 in conversation