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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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