BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Moksha
Pyrite | Level 9

Hi All, 

       I have deleted my earlier message content as I feel that it's not very clear and also received a response that it's not clear. Can anyone share sample code for date imputations where the input to a macro is a list of char type date variables?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

I've seen your initial post which was an example of "over-macrotized". 

You need to start with a single static example just using a SAS data step and make this work for all the different date patterns you're dealing with.

Only then try to make your code dynamic.

From how I understand your problem there shouldn't be a need for much macro logic at all.

 

You could start with a simple data step as below until you get it right for all the possible date strings you're dealing with.

data test;
  infile datalines truncover;
  input date_string $20.;
  format dt date9.;
  
  /* try-catch with multiple informats */
  dt=input(date_string,?? date9.);
  if missing(dt) then dt=input(date_string,?? yymmdd10.);
  if missing(dt) then dt=input(date_string,?? ddmmyy10.);
  /* more formats as required to cover all the possible date strings */
  datalines;
10Jan2023
10-Feb
20231015
15102023
15-10-2023
;

proc print data=test;
run;

 

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20

Your're right, it's a bit hard to follow you... 🙂

But if you could provide with a relevant input data it might becomes more clear. That in a for of a data step with DATALINES.

Data never sleeps
Moksha
Pyrite | Level 9

Thank you for the reply. Since, I feel that it's not very clear, I will delete this post.

Patrick
Opal | Level 21

I've seen your initial post which was an example of "over-macrotized". 

You need to start with a single static example just using a SAS data step and make this work for all the different date patterns you're dealing with.

Only then try to make your code dynamic.

From how I understand your problem there shouldn't be a need for much macro logic at all.

 

You could start with a simple data step as below until you get it right for all the possible date strings you're dealing with.

data test;
  infile datalines truncover;
  input date_string $20.;
  format dt date9.;
  
  /* try-catch with multiple informats */
  dt=input(date_string,?? date9.);
  if missing(dt) then dt=input(date_string,?? yymmdd10.);
  if missing(dt) then dt=input(date_string,?? ddmmyy10.);
  /* more formats as required to cover all the possible date strings */
  datalines;
10Jan2023
10-Feb
20231015
15102023
15-10-2023
;

proc print data=test;
run;

 

Moksha
Pyrite | Level 9

Thank you very much for the example. Yes, I will explore it further.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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