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.
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;
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;
Thank you so much. solution worked
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;
Thank you so much. solution worked
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.