I have a variable with dates in different formats and ranges. The first one is pretty simple. Looking for a way to scan and extract text for the latest date from the other variations so that I end up with the dates in Want and the Format. Please advise. Thanks!
| Have | Want | Format |
| Jan 9, 2018 | 20180109 | YYMMDDN8 |
| April 19,20,25, 2018 | 20180425 | YYMMDDN8 |
| June 7 & 11, 2018 | 20180611 | YYMMDDN8 |
| July 4 - 16, 2018 | 20180716 | YYMMDDN8 |
| Feb 26 - Mar 7, 2018 | 20180307 | YYMMDDN8 |
Like this:
data have;
input Have $ 20. Want yymmdd10. Format $ 10.;
format Want yymmddn8.;
cards;
Jan 9, 2018 20180109 YYMMDDN8
April 19,20,25, 2018 20180425 YYMMDDN8
June 7 & 11, 2018 20180611 YYMMDDN8
July 4 - 16, 2018 20180716 YYMMDDN8
Feb 26 - Mar 7, 2018 20180307 YYMMDDN8
;
run;
data want;
set have;
length m $ 3;
m = scan(compress(Have,,'KAS'), -1);
y = scan(Have, -1);
d = scan(Have, -2);
want_new = input(cats(d,m,y), date9.);
format want_new yymmddn8.;
compare = (want_new = want);
run;
proc print;
run;
?
Like this:
data have;
input Have $ 20. Want yymmdd10. Format $ 10.;
format Want yymmddn8.;
cards;
Jan 9, 2018 20180109 YYMMDDN8
April 19,20,25, 2018 20180425 YYMMDDN8
June 7 & 11, 2018 20180611 YYMMDDN8
July 4 - 16, 2018 20180716 YYMMDDN8
Feb 26 - Mar 7, 2018 20180307 YYMMDDN8
;
run;
data want;
set have;
length m $ 3;
m = scan(compress(Have,,'KAS'), -1);
y = scan(Have, -1);
d = scan(Have, -2);
want_new = input(cats(d,m,y), date9.);
format want_new yymmddn8.;
compare = (want_new = want);
run;
proc print;
run;
?
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.