DATA Step, Macro, Functions and more

Week date input problem

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Week date input problem

Please advise how to readin following week date.

Data Test;
input weekdate ; 
format weekdate	DTWKDATX25.;  
datalines; 
Saturday, March 31, 2018
Monday April 2, 2018
;
Run;

Accepted Solutions
Solution
‎04-23-2018 10:03 AM
Super User
Posts: 10,571

Re: Week date input problem

[ Edited ]
Posted in reply to emrancaan

You need to remove the redundant weekday first with substr(), then anydtdte will recognize the date:

data test;
input datestr $30.;
i = index(datestr,' ');
date = input(substr(datestr,i),anydtdte30.);
drop i;
format date weekdate30.;
cards;
Saturday March 31, 2018
;
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 10,571

Re: Week date input problem

Posted in reply to emrancaan

You only have dates, so a datetime format won't work.

 

The anydtdte informat can read a written (US style) date:

data test;
input date anydtdte30.;
format date yymmddd10.;
cards;
march 31,2018
;
run;

You need to remove the weekday first.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 36

Re: Week date input problem

Posted in reply to KurtBremser

Hi Kurt,

 

I want to read Saturday March 31, 2018 and store it in date type variable as it is.

 

Solution
‎04-23-2018 10:03 AM
Super User
Posts: 10,571

Re: Week date input problem

[ Edited ]
Posted in reply to emrancaan

You need to remove the redundant weekday first with substr(), then anydtdte will recognize the date:

data test;
input datestr $30.;
i = index(datestr,' ');
date = input(substr(datestr,i),anydtdte30.);
drop i;
format date weekdate30.;
cards;
Saturday March 31, 2018
;
run;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 122 views
  • 1 like
  • 2 in conversation