Help using Base SAS procedures

Date conversion

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Date conversion

In my dataset, I have two similar variables filled with dates. The informat is DATETIME19. After I merge these two variables with: 

 

DATA BCMasterSet3; SET BCMasterSet2;
IF DateOfInitDx1 = "." then DateInit = put(DateOfInitDx, DATETIME19.);
ELSE DateInit = DateOfInitDx1;
RUN;

SAS converts the format of the new DateInit variable to $19 and outputs some of the dates correctly and others like:         1449014400

 

What is that? Why didn't SAS create the DateInit variable with the data type DATETIME19. like I specified? How do I fix this and make it so all dates are in DATETIME19.? Because I have tried: 

 

Data BCMasterSetTime; SET BCMasterSet3;
format DateInit DATETIME19.;
RUN;

and was given the following message: 

667  Data BCMasterSetTime; SET BCMasterSet3;
668  format DateInit DATETIME19.;
                     -----------
                     48
ERROR 48-59: The format $DATETIME was not found or could not be loaded.

669  RUN;

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

 

So, any help is appreciated. Thanks


Accepted Solutions
Solution
‎06-06-2017 12:35 PM
Super User
Posts: 10,876

Re: Date conversion

DateInit = put(DateOfInitDx, DATETIME19.);

creates DateInit as character as PUT creates character variables when used that way. Think Print when using put.

 

if you want to copy a value then just use =

DateInit = DateOfInitDx;

You would use a Format or ATTRIB statement to associate the desired format.

Format DateInit datetime19.;

 

The error here:

668  format DateInit DATETIME19.;
                     -----------
                     48
ERROR 48-59: The format $DATETIME was not found or could not be loaded.

is because DateInit is Character. Datetime19 is a numeric format and hence cannot be applied to a character value. SAS looks for a defined character format, which would be $datetime because character variables required character formats. You likely have not run proc format to create a character format named datetime on so SAS could not find it.

 

 

And on a personal peeve note: Do not confuse DATE and DATETIME values. You have Datetimes, please call them that. Functions to manipulate dates and datetimes are different mainly because they are valued very differently. If you later ask a question like "I have a date variable and need to find the day of the week then the immediate response of MyWeekDay = Weekday(datevariable) will almost always get you an "invalid argument" note because Weekday and other date functions expect the date variable to be such. Datetime variables are counts of seconds and dates are counts of days. 1 day = 86400 so the range of values expected for the date functions blow up. I day in a datetime value will represent almost 236 years.

View solution in original post


All Replies
Super User
Posts: 18,589

Re: Date conversion

What is the current format and type of the variable?

And post an example of what it looks like.

Solution
‎06-06-2017 12:35 PM
Super User
Posts: 10,876

Re: Date conversion

DateInit = put(DateOfInitDx, DATETIME19.);

creates DateInit as character as PUT creates character variables when used that way. Think Print when using put.

 

if you want to copy a value then just use =

DateInit = DateOfInitDx;

You would use a Format or ATTRIB statement to associate the desired format.

Format DateInit datetime19.;

 

The error here:

668  format DateInit DATETIME19.;
                     -----------
                     48
ERROR 48-59: The format $DATETIME was not found or could not be loaded.

is because DateInit is Character. Datetime19 is a numeric format and hence cannot be applied to a character value. SAS looks for a defined character format, which would be $datetime because character variables required character formats. You likely have not run proc format to create a character format named datetime on so SAS could not find it.

 

 

And on a personal peeve note: Do not confuse DATE and DATETIME values. You have Datetimes, please call them that. Functions to manipulate dates and datetimes are different mainly because they are valued very differently. If you later ask a question like "I have a date variable and need to find the day of the week then the immediate response of MyWeekDay = Weekday(datevariable) will almost always get you an "invalid argument" note because Weekday and other date functions expect the date variable to be such. Datetime variables are counts of seconds and dates are counts of days. 1 day = 86400 so the range of values expected for the date functions blow up. I day in a datetime value will represent almost 236 years.

Contributor
Posts: 24

Re: Date conversion

This worked. Sorry I didn't get back sooner. Have been out of work due to illness =(
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 211 views
  • 2 likes
  • 3 in conversation