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

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
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

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;

?

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Amethyst | Level 16

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;

?

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



AP718
Obsidian | Level 7
Thank you! This worked to convert the dates!

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