DATA Step, Macro, Functions and more

Date formatting

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Date formatting

Hi SAS Community,

 

I am trying to solve this issue with date formatting. I just want to make the Enddate variable in a date format. I tried anydtdte and worddate. But all those end up with an error that the format doesnot exist.

 

 

data abcd ;
set abc ;
Informat Enddate : anydtdte20.;
run;

 

 

Code Enddate

A4466December 31, 2016
A7011December 31, 2015
A7042December 31, 2014
A7043December 31, 2014
A9544December 31, 2016
A9545December 31, 2016
B9000December 31, 2016
C1204December 31, 2013
C1300December 31, 2014
C1879June 30, 2013

 

 

Any help much appreciated.


Accepted Solutions
Solution
a month ago
Super User
Super User
Posts: 7,083

Re: Date formatting

If you are working from an existing character variable, instead of a reading from a source text file, then you will want to use the INPUT() function instead of the INPUT statement.  You will need to create a new numeric variable to store the date as you cannot store it back into the character variable.

data abcd ;
  set abc ;
  datevar=input(Enddate,anydtdte20.);
  format datevar date9.;
run;

View solution in original post


All Replies
Super Contributor
Posts: 345

Re: Date formatting

You can't change the type of a variable, you have to use the correct informat when reading data.

data have;

input Code $5. @7 Enddate anydtdte20.;

format Enddate date9.;

datalines;
A4466	December 31, 2016
A7011	December 31, 2015
A7042	December 31, 2014
A7043	December 31, 2014
A9544	December 31, 2016
A9545	December 31, 2016
B9000	December 31, 2016
C1204	December 31, 2013
C1300	December 31, 2014
C1879	June 30, 2013
;
run;
Occasional Contributor
Posts: 15

Re: Date formatting

Posted in reply to andreas_lds

Hello Andreas. This is the log i get when SAS is run. The End date is in $31 format if it helps for the soulution.

data abcd ;
118 set abc ;
119 input Code $5. @7 Enddate anydtdte20.;
-----------
48
ERROR 48-59: The informat $ANYDTDTE was not found or could not be loaded.

120 format Enddate date9.;
------

ERROR 48-59: The format $DATE was not found or could not be loaded.

121 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.ABCD may be incomplete. When this step was stopped there were 0
observations and 2 variables.
WARNING: Data set WORK.ABCD was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

 

Solution
a month ago
Super User
Super User
Posts: 7,083

Re: Date formatting

If you are working from an existing character variable, instead of a reading from a source text file, then you will want to use the INPUT() function instead of the INPUT statement.  You will need to create a new numeric variable to store the date as you cannot store it back into the character variable.

data abcd ;
  set abc ;
  datevar=input(Enddate,anydtdte20.);
  format datevar date9.;
run;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 97 views
  • 1 like
  • 3 in conversation