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
Onyx | Level 15

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
Onyx | Level 15

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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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