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

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
Quartz | Level 8

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
Quartz | Level 8

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

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 225 views
  • 2 likes
  • 3 in conversation